-
Notifications
You must be signed in to change notification settings - Fork 1
/
netconf.py
120 lines (90 loc) · 3.04 KB
/
netconf.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
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
#!usr/bin/python3
import random
import time
import pycurl
import json
import os
from subprocess import Popen, PIPE
try:
# python 3
from urllib.parse import urlencode
from io import BytesIO
except ImportError:
# python 2
from urllib import urlencode
from StringIO import StringIO as BytesIO
# Minute interval to update network settings
update_interval = 0.1
# The UUID is initialized from the device configuration
uuid = 'invalid'
# The idea is there will be more entry points for things like bee/camerafeed ..etc
base = 'http://localhost:3420/'
# Read the UUID from the beemon file
beemon = open("beemon.cfg", 'r')
for line in beemon:
if line[0] == '#': continue
param = line.split('=')[0]
value = line.split('=')[1]
if param == "uuid":
uuid = value.strip()
elif param == "endpoint":
base = value.strip()
print("UUID: " + uuid)
print("Endpoint: " + base)
netupdate_url = base + '/bee/network'
wireless_interface = 'wlan0'
def update():
c = pycurl.Curl()
c.setopt(c.URL, netupdate_url)
c.setopt(c.WRITEFUNCTION, buffer.write)
available_ssids = []
network_types = []
# Get the available networks and send their SSID and type
settingLine = True
pipe = Popen("iwlist " + wireless_interface + " s | grep -E 'SSID|Encryption'", shell=True, stdout=PIPE)
for index, line in enumerate(pipe.stdout):
if not settingLine:
ssid = line.strip().split(b':')[1][1:-1]
if ssid:
available_ssids.append(ssid)
else:
network_types.append(line.strip().split(b':')[1])
settingLine = not settingLine
available_networks = []
for index, ssid in enumerate(available_ssids):
available_networks.append({
'ssid' : ssid.decode('utf-8'),
'type' : network_types[index].decode("utf-8")
})
post_data = {'uuid': uuid, 'networks': json.dumps(available_networks)}
c.setopt(c.POSTFIELDS, urlencode(post_data))
c.perform()
http_response_code = c.getinfo(pycurl.HTTP_CODE)
c.close()
return http_response_code
def connectToNetwork(network):
print("Connecting to new network: " + network['ssid'])
temp_conf_file = open('wpa_supplicant.conf.tmp', 'w')
temp_conf_file.write('\n')
temp_conf_file.write('network={\n')
temp_conf_file.write(' ssid="' + network['ssid'] + '"\n')
if network['ssid'] == '':
temp_conf_file.write(' key_mgmt=NONE\n')
else:
temp_conf_file.write(' psk="' + network['passcode']+ '"\n')
temp_conf_file.write(' }')
temp_conf_file.close
os.system('cat wpa_supplicant.conf.tmp >> /etc/wpa_supplicant/wpa_supplicant.conf')
os.system('reboot')
while True:
try:
buffer = BytesIO()
http_code = update()
response = buffer.getvalue()
jsonResponse = json.loads(response)
if jsonResponse['active'] == 1:
connectToNetwork(jsonResponse)
except pycurl.error:
print("Error contacting server!")
continue
time.sleep(update_interval*60)