forked from Screenly/Anthias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_resin_wifi.py
57 lines (42 loc) · 1.8 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
#!/usr/bin/python
from __future__ import unicode_literals
from builtins import filter
from builtins import str
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 = [c for c in [c for c in wireless_connections if pattern_include.search(str(c['Devices']))] if not pattern_exclude.search(str(c['Id']))]
if not gateways().get('default') and list(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 list(filter(pattern_include.match, interfaces())):
time.sleep(.5)