-
Notifications
You must be signed in to change notification settings - Fork 1
/
raspi-wlan.py
executable file
·168 lines (151 loc) · 6.31 KB
/
raspi-wlan.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
import os,sys,time
print "RaspberryPi wlan auto connecter\n" \
"Copyright (c) 2015 winlin(winlin@vip.126.com)"
if len(sys.argv) <= 2:
print "Usage: %s <ssid0> <passwd0> [<ssidN> <passwdN>]\n" \
" ssid the ssid of AP or other wireless device.\n" \
" passwd the psk for AP which use WPA-PSK or WPA2-PSk.\n" \
"For example:\n" \
" %s MyApSsid PlainPasswd\n" \
" %s MyApSsid0 PlainPasswd0 MyApSsidN PlainPasswdN\n" \
" %s 'My AP SSID' 'Plain Passwd'"%(
sys.argv[0], sys.argv[0], sys.argv[0], sys.argv[0]
)
sys.exit(-1)
if (len(sys.argv) % 2) == 0:
print "Error: please specifies the passwd for ssid %s."%(sys.argv[len(sys.argv) - 1])
sys.exit(-1)
class ApConfig:
def __init__(self, ssid, psk):
self.ssid = str(ssid).strip('"')
self.psk = psk
class DeviceConfig:
def __init__(self, ifname):
self.ifname = ifname
self.ip = None
aps = []
for i in range(1, len(sys.argv), 2):
ap = ApConfig(sys.argv[i], sys.argv[i + 1])
print "Add ap(%s, %s)"%(ap.ssid, ap.psk)
aps.append(ap)
if len(aps) == 0:
print "No ap specified."
sys.exit(-1)
def iwlist_scan(wlan, aps):
with os.popen("iwlist %s scan 2>/dev/null|grep ESSID|awk -F ':' '{print $2}'"%(wlan.ifname)) as f:
all_ap = f.read()
for ap in aps:
ssid = '"%s"'%(ap.ssid)
if ssid in all_ap:
return ap
return None
def ifconfig_scan(wlans):
for wlan in wlans:
wlan.ip = None
for wlan in wlans:
with os.popen("ifconfig %s 2>/dev/null|grep 'inet addr'|awk -F ':' '{print $2}'|awk '{print $1}'"%(wlan.ifname)) as f:
ip = f.read().strip()
if len(ip) > 0:
wlan.ip = ip
return wlan
return None
while True:
# detect the device, maybe changed.
wlans = []
while True:
with os.popen("cat /proc/net/wireless 2>/dev/null|awk '{print $1}'|awk -F ':' '{print $1}'|grep 'wlan'") as f:
for wlan in f.read().splitlines():
device = DeviceConfig(wlan)
print "Add device(%s) to config"%(device.ifname)
wlans.append(device)
if len(wlans) == 0:
print "No wlan found."
time.sleep(1)
continue
break
# retrieve ip for all wlans.
wlan = ifconfig_scan(wlans)
if wlan is not None:
print "Wlan %s is ok, ip is %s"%(wlan.ifname, wlan.ip)
time.sleep(30)
continue
# got the ap to connect.
ap = None
for wlan in wlans:
ap = iwlist_scan(wlan, aps)
if ap is not None:
break
# no ap found.
if ap is None:
print "No ap found."
time.sleep(10)
continue
print "Config wlan for ap(%s) and retrieve ip."%(ap.ssid)
for wlan in wlans:
try:
nid = None
with os.popen("sudo wpa_cli -i%s list_networks 2>/dev/null |grep ' %s '| awk '{print $1}'"%(wlan.ifname, ap.ssid)) as f:
nid = f.read().strip()
if len(nid) > 0:
print "Use network %s for %s"%(nid, wlan.ifname)
else:
print "Create network for %s"%(wlan.ifname)
if nid is None or len(nid) == 0:
with os.popen("sudo wpa_cli -i%s add_network 2>/dev/null"%(wlan.ifname)) as f:
nid = f.read().strip()
if len(nid) == 0:
print "Add network for %s failed."%(wlan.ifname)
continue
print "Add network %s for %s ok."%(nid, wlan.ifname)
with os.popen("""sudo wpa_cli -i%s set_network %s ssid '"%s"' 2>/dev/null"""%(wlan.ifname, nid, ap.ssid)) as f:
ret = f.read().strip()
if ret != "OK":
print "Set network %s ssid to %s for %s failed."%(nid, ap.ssid, wlan.ifname)
continue
print "Set network %s ssid %s for %s ok."%(nid, ap.ssid, wlan.ifname)
with os.popen("""sudo wpa_cli -i%s set_network %s psk '"%s"' 2>/dev/null"""%(wlan.ifname, nid, ap.psk)) as f:
ret = f.read().strip()
if ret != "OK":
print "Set network %s psk to %s for %s failed."%(nid, ap.psk, wlan.ifname)
continue
print "Add network %s psk %s for %s ok."%(nid, ap.psk, wlan.ifname)
with os.popen("sudo wpa_cli -i%s enable_network %s 2>/dev/null"%(wlan.ifname, nid)) as f:
ret = f.read().strip()
if ret != "OK":
print "Enable network %s for %s failed."%(nid, wlan.ifname)
continue
print "Enable network %s for %s ok."%(nid, wlan.ifname)
ok = False
for i in range(0, 10, 1):
with os.popen("sudo wpa_cli -i%s status 2>/dev/null|grep 'wpa_state'|awk -F'=' '{print $2}'"%(wlan.ifname)) as f:
ret = f.read().strip()
print "Status %s is %s"%(wlan.ifname, ret)
if ret == "COMPLETED":
ok = True
break
time.sleep(3)
if ok:
ok = False
for i in range(0, 20, 1):
twlan = ifconfig_scan(wlans)
if twlan is not None:
ok = True
break
print "Wait %s to dispatch ip."%(wlan.ifname)
time.sleep(3)
if ok:
# ok, set to None to keep it.
print "Config network %s for %s ok, ip is %s."%(nid, wlan.ifname, wlan.ip)
nid = None
else:
print "Config network %s ap(%s,%s) for %s failed."%(nid, ap.ssid, ap.psk, wlan.ifname)
finally:
if nid is not None and len(nid) > 0:
with os.popen("sudo wpa_cli -i%s remove_network %s 2>/dev/null"%(wlan.ifname, nid)) as f:
ret = f.read().strip()
if ret != "OK":
print "Remove network %s for %s failed."%(nid, wlan.ifname)
else:
print "Remove netowrk %s for %s ok"%(nid, wlan.ifname)
print "Refresh state of wlan for RaspberryPi."