forked from Screenly/Anthias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_resin_wifi.py
60 lines (45 loc) · 1.76 KB
/
start_resin_wifi.py
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
#!/usr/bin/python
import time
import pydbus
import re
import sh
from jinja2 import Template
from netifaces import gateways, interfaces
from os import getenv, path
from lib.utils import generate_perfect_paper_password, get_active_connections
def generate_page(ssid, pswd, address):
home = getenv('HOME')
template_path = path.join(home, 'screenly/templates/hotspot.html')
with open(template_path) as f:
template = Template(f.read())
context = {
'network': ssid,
'ssid_pswd': pswd,
'address': address
}
with open('/tmp/hotspot.html', 'w') as out_file:
out_file.write(template.render(context=context))
if __name__ == "__main__":
bus = pydbus.SystemBus()
pattern_include = re.compile("wlan*")
pattern_exclude = re.compile("ScreenlyOSE-*")
wireless_connections = get_active_connections(bus)
if wireless_connections is None:
exit()
wireless_connections = filter(
lambda c: not pattern_exclude.search(str(c['Id'])),
filter(
lambda c: pattern_include.search(str(c['Devices'])),
wireless_connections
)
)
if not gateways().get('default') and filter(pattern_include.match, interfaces()):
if len(wireless_connections) == 0:
ssid = 'ScreenlyOSE-{}'.format(generate_perfect_paper_password(pw_length=4, has_symbols=False))
ssid_password = generate_perfect_paper_password(pw_length=8, has_symbols=False)
generate_page(ssid, ssid_password, 'screenly.io/wifi')
wifi_connect = sh.sudo('wifi-connect', '-s', ssid, '-p', ssid_password, '-o', '9090', _bg=True)
else:
exit()
while not gateways().get('default') and filter(pattern_include.match, interfaces()):
time.sleep(.5)