This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifisetup
executable file
·90 lines (73 loc) · 2.47 KB
/
wifisetup
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
#!/usr/bin/env python
from __future__ import absolute_import, division, print_function
import os
import re
import dbus
NM_NAME_PREFIX = 'org.freedesktop.NetworkManager'
NM_PATH_PREFIX = '/org/freedesktop/NetworkManager'
def get_configured_networks():
"""Return a list of configured SSIDs
(Assume that SSID is a sufficient identifier)
This will start its own GLib event loop, so don't call it if there's
already one.
"""
ssids = []
bus = dbus.SystemBus()
settings_proxy = bus.get_object(
NM_NAME_PREFIX, NM_PATH_PREFIX + '/Settings')
connections = (dbus.Interface(settings_proxy, dbus.PROPERTIES_IFACE).
Get(NM_NAME_PREFIX + '.Settings', 'Connections'))
for path in connections:
connection_proxy = bus.get_object(
NM_NAME_PREFIX, path)
connection_settings = (dbus.Interface(
connection_proxy, NM_NAME_PREFIX + '.Settings.Connection').
GetSettings(byte_arrays=True))
if connection_settings['connection']['type'] != '802-11-wireless':
continue
ssids.append(str(connection_settings['802-11-wireless']['ssid']))
return ssids
def create_network(ssid, psk):
conn = {
'connection': {
'id': 'wifisetup-{}'.format(re.sub('[^0-9A-Za-z]+', '', ssid)),
'type': '802-11-wireless',
},
'802-11-wireless': {
'ssid': dbus.ByteArray(ssid),
'hidden': True, # just in case
},
'ipv4': {
'method': 'auto',
},
'ipv6': {
'method': 'auto',
},
}
if psk:
conn['802-11-wireless-security'] = {
'auth-alg': 'open',
'key-mgmt': 'wpa-psk',
'psk': psk,
}
bus = dbus.SystemBus()
settings_proxy = bus.get_object(
NM_NAME_PREFIX, NM_PATH_PREFIX + '/Settings')
dbus.Interface(
settings_proxy, NM_NAME_PREFIX + ".Settings").AddConnection(conn)
def main():
actual_networks = set(get_configured_networks())
# look for networks in the environment
desired_networks = []
for k, v in os.environ.items():
if not k.startswith('WIFISETUP_'):
continue
ssid = k[len('WIFISETUP_'):]
if ssid in actual_networks:
continue
psk = v if v != '' else None
desired_networks.append((ssid, psk))
for (ssid, psk) in desired_networks:
create_network(ssid, psk)
if __name__ == '__main__':
main()