/usr/share/checkbox/scripts/create_connection is in checkbox 0.13.7.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #!/usr/bin/env python
import sys
import os
import time
from subprocess import Popen, PIPE
from uuid import uuid4
from optparse import OptionParser
CONNECTIONS_PATH='/etc/NetworkManager/system-connections/'
def connection_section(ssid, uuid):
if not uuid: uuid = uuid4()
connection = """
[connection]
id=%s
uuid=%s
type=802-11-wireless
""" % (ssid, uuid)
wireless = """
[802-11-wireless]
ssid=%s
mode=infrastructure""" % (ssid)
return connection + wireless
def security_section(security, key):
# Add security field to 802-11-wireless section
wireless_security = """
security=802-11-wireless-security
[802-11-wireless-security]
"""
if security.lower() == 'wpa':
wireless_security += """
key-mgmt=wpa-psk
auth-alg=open
psk=%s
""" % key
elif security.lower() == 'wep':
wireless_security += """
key-mgmt=none
wep-key=%s
""" % key
return wireless_security
def ip_sections():
ip = """
[ipv4]
method=auto
[ipv6]
method=auto
"""
return ip
def block_until_created(connection, retries, interval):
while retries > 0:
nmcli_con_list = Popen(['nmcli','con','list'], stdout=PIPE)
(stdout, stderr) = nmcli_con_list.communicate()
if connection in stdout:
print("Connection %s registered" % connection)
break
time.sleep(interval)
retries = retries - 1
if retries <= 0:
print("Failed to register %s." % connection)
sys.exit(1)
else:
nmcli_con_up = Popen(['nmcli','con','up','id', connection], stdout=PIPE)
(stdout, stderr) = nmcli_con_up.communicate()
if 'state: activated' in stdout:
print("Connection %s activated." % connection)
else:
print("Failed to activate %s." % connection)
sys.exit(1)
def main():
parser = OptionParser()
parser.add_option('-S', '--security',
help="""The type of security to be used by the connection.
One of wpa and wep. No security will be used if
nothing is specified.""")
parser.add_option('-K', '--key',
help="The encryption key required by the router.")
parser.add_option('-U', '--uuid',
help="""The uuid to assign to the connection for use by
NetworkManager. One will be generated if not
specified here.""")
parser.add_option('-R', '--retries',
help="""The number of times to attempt bringing up the
connection until it is confirmed as active.""",
default=5)
parser.add_option('-I', '--interval',
help="""The time to wait between attempts to detect the
registration of the connection.""",
default=2)
(options, args) = parser.parse_args()
if len(args) < 1:
print("Must specify an SSID to connect to.")
sys.exit(1)
connection_info = connection_section(args[0], options.uuid)
if options.security:
# Set security options
if not options.key:
print "You need to specify a key using --key if using wireless security."
sys.exit(1)
connection_info += security_section(options.security, options.key)
elif options.key:
print "You specified an encryption key but did not give a security type using --security."
sys.exit(1)
connection_info += ip_sections()
try:
connection_file = open(CONNECTIONS_PATH + args[0], 'w')
connection_file.write(connection_info)
os.fchmod(connection_file.fileno(), 0600)
connection_file.close()
except IOError:
print "Can't write to " + CONNECTIONS_PATH + args[0] + ". Is this command being run as root?"
sys.exit(1)
block_until_created(args[0], options.retries, options.interval)
if __name__ == "__main__":
main()
|