2 from package.bottle import *
5 # Refer https://segmentfault.com/a/1190000011579147
7 DIR = os.path.dirname(os.path.realpath(__file__))
8 CONF = '/usr/local/trackerpp/config/trackerpp.conf'
9 SERVICE = 'trackerpp.service'
11 TEMPLATE_PATH.insert(0, DIR + '/views/')
15 return subprocess.check_output("iw dev | grep Interface | awk '{print $2}'", shell=True).strip()
17 def getConnectedSSID():
18 return subprocess.check_output("iw dev | grep ssid | awk '{print $2}'", shell=True).strip()
21 return subprocess.check_output("ip addr", shell=True).strip()
25 return subprocess.check_output("iw dev | grep type | awk '{print $2}'", shell=True).strip()
28 return getCurrentMode() == 'AP'
33 cmd = "iw dev " + wlan + " scan | grep -i ssid | awk '{print $2}'"
34 return subprocess.check_output(cmd, shell=True).strip().split('\n')
36 def getTrackerPPConf(fname = CONF):
37 with open(fname, 'rt') as f:
40 def updateTrackerPPConf(content, fname = CONF):
41 with open(fname, 'wt') as f:
44 def restartTrackerPPService():
45 subprocess.check_call("systemctl restart trackerpp.service", shell=True)
47 def getCurrentNetworkId():
48 return subprocess.check_output("wpa_cli -i wlan0 list_networks | grep -i current | awk '{print $1}'", shell=True).strip()
50 def connectWifi(ssid, passwd):
52 print 'stop hostapd first'
53 subprocess.check_output( DIR + "/script/stopHostapd.sh", shell=True)
55 cmd = "wpa_cli -i wlan0 add_network"
57 id = subprocess.check_output(cmd, shell=True).strip()
59 cmd = "wpa_cli -i wlan0 set_network " + id + " ssid '\"" + ssid + "\"'"
61 subprocess.check_output(cmd, shell=True)
63 cmd = "wpa_cli -i wlan0 set_network " + id + " psk '\"" + passwd + "\"'"
65 subprocess.check_output(cmd, shell=True)
67 cmd = "wpa_cli -i wlan0 disable_network " + getCurrentNetworkId()
69 subprocess.check_output(cmd, shell=True)
71 cmd = "wpa_cli -i wlan0 enable_network " + id
73 subprocess.check_output(cmd, shell=True)
77 cmd = "wpa_cli -i wlan0 disable_network " + getCurrentNetworkId()
79 subprocess.check_output(cmd, shell=True)
80 subprocess.check_output(DIR + "/script/startHostapd.sh", shell=True)
85 @get('/login') # or @route('/login')
87 return template('login.tpl', username="username", password="password")
89 def check_login(username, password):
91 #return username == 'admin' and password == 'admin123'
93 form_dict = {'ipaddr': getIpAddr(),
95 'usedSsid': getConnectedSSID(),
100 action = request.forms.get('action')
101 if action == "Login":
102 username = request.forms.get('username')
103 password = request.forms.get('password')
104 if not check_login(username, password):
105 return "<p>Login failed.</p>"
107 form_dict['ipaddr'] = getIpAddr()
108 form_dict['usedSsid'] = getConnectedSSID()
109 form_dict['mode'] = 'AP' if isAPmode() else 'WiFi'
110 print getConnectedSSID()
111 print form_dict['usedSsid']
112 form_dict['ssids'] = getSSIDs()
113 elif action == 'Scan':
114 form_dict['usedSsid'] = getConnectedSSID()
115 form_dict['ssids'] = getSSIDs()
116 elif action == 'Connect':
117 selectedSsid = request.forms.get('select')
118 password = request.forms.get('password')
119 return connectWifi(selectedSsid, password)
120 elif action == 'Reset':
122 return template('main.tpl', **form_dict)
125 run(host='0.0.0.0', port=80)