-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifiap_evaluate2.sh
executable file
·364 lines (323 loc) · 6.83 KB
/
wifiap_evaluate2.sh
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/bin/bash
# This script has some wlan utility functions using iw and etc, basically to evaluate wireless lan AP in the radio range.
# Tested on Rasbian stretch lite running on Raspberry Pi 3B+
# Usage:
# wifiap_evaluate2.sh scan interface ssid
# wifiap_evaluate2.sh deassociate interface
# wifiap_evaluate2.sh associate interface ssid passwd
# wifiap_evaluate2.sh connect interface ip_address
# wifiap_evaluate2.sh connect_internet interface
# wifiap_evaluate2.sh regulatory_domain interface country_code
# wifiap_evaluate2.sh refresh_netif interface
# brcmfmac unload
function mac_unload()
{
while :
do
modprobe -r brcmfmac
modprobe -r cfg80211
if [ $? -eq 0 ]; then
echo "brcmfmac/cfg80211 is unloaded."
break
fi
sleep 1
done
sleep 1
}
function mac_load()
{
while :
do
modprobe brcmfmac
if [ $? -eq 0 ]; then
echo "brcmfmac is loaded."
break
fi
sleep 1
done
sleep 1
}
function mac_refresh()
{
mac_unload
mac_load
}
# netif down
function netif_down()
{
systemctl stop dhcpcd
systemctl stop wpa_supplicant
sleep 1
ip link set $1 down
echo "$1 is down with wpa_supplicant and dhcpcd."
sleep 1
return 0
}
# netif up
function netif_up()
{
while :
do
ip link set $1 up
if [ $? -eq 0 ]; then
systemctl start wpa_supplicant
sleep 1
systemctl start dhcpcd
sleep 1
echo "$1 is up with wpa_supplicant and dhcpcd."
break
fi
sleep 1
done
return 0
}
# netif refresh
function refresh_netif()
{
netif_down $1
mac_unload
mac_load
netif_up $1
sleep 2
return 0
}
# reg set - deprecated. rather use refresh_netif to set to default reg. domain
function reg_set()
{
while :
do
iw reg set $1
if [ $? -eq 0 ]; then
echo "Reg domain sets to $1."
break
fi
sleep 1
done
sleep 3
return 0
}
# wpa reconfigure
function wpa_reconfig()
{
while :
do
wpa_cli -i $1 reconfigure
res=$?
if [ $res -eq 0 ]; then
echo "wpa is now reconfigured for $1."
break
fi
sleep 1
refresh_netif $1
done
sleep 1
return 0
}
# Scan ap and check if $2 is there
# Before executing scan, we need to make sure that wlan is not associated. Once associated, it's not allowed to scan other channels
function try_scan()
{
while :
do
res="$(iw $1 scan | grep SSID)"
if [ -z "$res" ]; then
sleep 1
else
if echo "$res" | grep -q "$2"; then
return 0
else
return 1
fi
fi
sleep 1
done
return 1
}
# Deassociate netif
function deassociate()
{
iw dev $1 link | grep "Not connected."
if [ $? -eq 0 ]; then
echo 'Currently not connected. No need to issue deassociate here.'
return 1
fi
if [ $? -eq 0 ]; then
text=$'ctrl_interface=DIR=/var/run/wpa_supplicant\n'
text+=$'update_config=1\n'
text+=$'country=UK\n'
text+=$'network={\n'
text+=$'\tscan_ssid=1\n'
text+=$'\tssid='
text+=$'\"This ssid is a non-existing.\"'
text+=$'\n'
text+=$'\tpsk='
text+=$'\"FAKEPASSWD\"'
text+=$'\n'
text+=$'}'
echo "${text[*]}" > /etc/wpa_supplicant/wpa_supplicant.conf
echo "wpa_reconfig 1"
wpa_reconfig $1
echo "wpa_reconfig 2"
fi
return $?
}
function try_associate()
{
local res=1
local count=0
text=$'ctrl_interface=DIR=/var/run/wpa_supplicant\n'
text+=$'update_config=1\n'
text+=$'country=UK\n'
text+=$'network={\n'
text+=$'\tscan_ssid=1\n'
text+=$'\tssid=\"'
text+=$2
text+=$'\"\n'
text+=$'\tpsk=\"'
text+=$3
text+=$'\"\n'
text+=$'}'
echo "${text[*]}" > /etc/wpa_supplicant/wpa_supplicant.conf
echo "wpa_reconfig 1"
wpa_reconfig $1
echo "wpa_reconfig 2"
while :; do
sleep 1
((count++))
iw $1 link | grep $2
res=$?
if [ $res -eq 0 ]; then
echo "$2 is associated at count $count."
break
fi
if [ $count -ge 30 ]; then
break
fi
done
return $res
}
function try_connect()
{
ping -c 1 -W 10 $2 > /dev/null 2>&1
return $?
}
# Check if we can connect to internet
function try_connect_internet()
{
local r=0
declare -a arr=("http://www.google.com" "http://www.facebook.com" "http://www.github.com")
for i in "${arr[@]}"
do
wget --spider --timeout=10 --quiet "$i"
r=$?
if [ $r -eq 0 ]; then
break;
#else
# echo "Can't connect. $?"
fi
done
return $r
}
# wifiap_evaluate2.sh scan interface ssid
if [ $1 == "scan" ]; then
try_scan $2 $3
if [ $? -eq 0 ]; then
echo "Okay, $3 is in the radio range."
exit 0
else
echo "$3 seems to be out of radio range."
exit 1
fi
# wifiap_evaluate2.sh dessociate interface
elif [ $1 == "deassociate" ]; then
deassociate $2
if [ $? -eq 0 ]; then
echo "Now deassociated with $2."
exit 0
else
echo "Can't deassociate $2."
exit 1
fi
# wifiap_evaluate2.sh associate interface ssid passwd
elif [ $1 == "associate" ]; then
echo 'associate'
try_associate $2 $3 $4
if [ $? -eq 0 ]; then
echo "Okay, now associated with $3."
exit 0
else
echo "Can't associate $3."
exit 1
fi
# wifiap_evaluate2.sh connect interface ip_address # try connect to given ip
# if ip_address is not given, then we try connect to default gw of given interface
elif [ $1 == "connect" ]; then
if [ "$#" -eq 2 ]; then
# see if we can see the gateway for the associated AP
ok=0
for run in {1..10}; do
sleep 1
netstat -rn | grep UG
if [ $? -eq 0 ]; then
ok=1; break
fi
done
if [ $ok -eq 0 ]; then
echo "Can't find gateway via $2."
exit 1
fi
# try to connect to gateway
gateway=`netstat -rn | grep UG | grep $2 | awk '{print $2}'`
try_connect $2 $gateway
if [ $? -eq 0 ]; then
echo "Okay, now connected to $gateway via $2."
exit 0
else
echo "Can't connect to $gateway via $2."
exit 1
fi
else
try_connect $2 $3
if [ $? -eq 0 ]; then
echo "Okay, now connected to $3 via $2."
exit 0
else
echo "Can't connect to $3 via $2."
exit 1
fi
fi
# wifiap_evaluate2.sh connect_internet interface
elif [ $1 == "connect_internet" ]; then
try_connect_internet $2
if [ $? -eq 0 ]; then
echo "Okay, now connected to internet via $2."
exit 0
else
echo "Can't connect to internet via $2."
exit 1
fi
# regulatory domain set
# some workaround is included due to the issue registered at https://github.com/raspberrypi/linux/issues/2997
elif [ $1 == "regulatory_domain" ]; then
reg_set $3
if [ $? -eq 0 ]; then
echo "Okay, regulatory domain is set to $3"
exit 0
else
echo "Can't set regulatory domain."
exit 1
fi
# refresh netif
elif [ $1 == "refresh_netif" ]; then
refresh_netif $2
if [ $? -eq 0 ]; then
echo "Okay, netif $2 is refreshed."
exit 0
else
echo "Can't refresh netif."
exit 1
fi
fi
# exception
exit 1
# eof