-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwlan_ network_switcher.sh
More file actions
80 lines (70 loc) · 2.93 KB
/
wlan_ network_switcher.sh
File metadata and controls
80 lines (70 loc) · 2.93 KB
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
#!/bin/sh
#This script switches between WLAN networks based on priority and by checking internet access, author: @gerarg
status_network1="$(ifstatus wwan | jsonfilter -e '@["up"]')"
status_network2="$(ifstatus wwan2 | jsonfilter -e '@["up"]')"
status_network3="$(ifstatus wwan3 | jsonfilter -e '@["up"]')"
number_of_processes="$(pgrep -f 'restart-dns'|wc -l)"
is_network1_disabled="$(uci get wireless.@wifi-iface[2].disabled)"
#Checking for active networks, otherwise we don't do anything
if { [ "$status_network1" = true ] || [ "$status_network2" = true ] || [ "$status_network3" = true ]; } && [ "$number_of_processes" -eq 2 ]; then
#Checking internet access
if wget -q --spider https://www.google.com > /dev/null; then
#We have internet access, we're gonna try to switch to network1 (provided network1 has internet access) and disable network 2 and 3
if ping -c4 -I wlan1 8.8.8.8 > /dev/null && [ "$status_network1" = true ] && { [ "$status_network2" = true ] || [ "$status_network3" = true ]; }; then
uci set wireless.@wifi-iface[3].disabled='1'
uci set wireless.@wifi-iface[4].disabled='1'
uci commit wireless
/sbin/wifi down radio1 && /sbin/wifi up radio1
logger -s "Detected internet access, switching to network 1"
exit 0
fi
#No internet access, we're going to try to fix it
else
[ ! -f "/tmp/number_of_errors" ] && number_of_errors=0 || number_of_errors=$(cat /tmp/number_of_errors)
#If we can't access internet 4 or more times in a 10 minute timespan, we're going to try the most user-disruptve fix by restarting the network service
if [ "$number_of_errors" -ge 4 ]; then
kill -9 "$(pgrep -f contador)"
kill -9 "$(pgrep -f 'sleep 600')"
/etc/init.d/network stop && /etc/init.d/network start
/etc/init.d/firewall stop && /etc/init.d/firewall start
/etc/init.d/dnsmasq stop && /etc/init.d/dnsmasq start
/etc/init.d/https-dns-proxy stop && /etc/init.d/https-dns-proxy start
logger -s "Restarted everything (but the router)"
echo 0 > /tmp/number_of_errors
exit 0
fi
#Trying to fix it by restarting the dns related services
/etc/init.d/dnsmasq stop && /etc/init.d/dnsmasq start
/etc/init.d/https-dns-proxy stop && /etc/init.d/https-dns-proxy start
logger -s "DNS services restarted"
echo "$(sleep 10)"
if wget -q --spider https://www.google.com > /dev/null; then
logger -s "Internet access recovered"
exit 0
else
/sbin/wifi up radio1
number_of_errors="$((number_of_errors + 1))"
echo "${number_of_errors}" > /tmp/number_of_errors
if [ "$number_of_errors" -eq 1 ]; then
(/usr/local/sbin/counter.sh)&
fi
logger -s "WiFi restarted"
echo "$(sleep 10)"
if ping -c 4 8.8.8.8 > /dev/null
then
logger -s "Internet access recovered"
exit 0
else
if [ "$status_network2" = false ]; then
uci delete wireless.@wifi-iface[3].disabled
uci commit wireless
/sbin/wifi down radio1 && /sbin/wifi up radio1
echo "$((number_of_errors + 1))" > /tmp/number_of_errors
logger -s "Switched to network2"
exit 0
fi
fi
fi
fi
fi
exit 0