-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection-keeper.sh
92 lines (77 loc) · 2.59 KB
/
connection-keeper.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
#!/bin/bash
### SETTINGS BEGIN ###
# Adding folder with "sakis3gz" to PATH variable
PATH=$PATH:/home/pi/connection-keeper
# Set this as default gateway for wired connection
# Run "route | grep default" to learn default gateway
# while connected to wired network
primary_gw="192.168.0.1"
# Wired network interface name
ethernet="eth0"
# Settings for connection to internet with "sakis3g"
sakis3g_options="APN=internet.nw USBDRIVER=option MODEM=12d1:1001"
# Connection state check period in seconds
check_delay=60
### SETTINGS END ###
# DON'T CHANGE
ping_address="8.8.8.8"
ping_command="ping -c 1 -W 1 $ping_address |& \
grep -E \"(unreachable|100\%\ packet\ loss)\" &> /dev/null"
route_via_gw="ip route add $ping_address via $primary_gw &> /dev/null"
function change_to_primary {
sakis3gz disconnect
# This is required to get name resolution working properly
ifdown $ethernet
ifup $ethernet
}
function change_to_secondary {
sakis3gz connect --pppd $sakis3g_options
}
function connection_checker {
# First we check internet connection
if eval $ping_command ;then
# If we don't have internet
if [ -e /tmp/wan_backup ] ;then
# If we are using backup right now we try to change to primary connection
echo "Secondary connection was lost, trying to switch back to primary"
change_to_primary
rm /tmp/wan_backup
else
# Else we change to wan backup.
echo "Primary connection was lost, switching to backup"
change_to_secondary
touch /tmp/wan_backup
fi
else
echo -n "Current connection"
if [ -e /tmp/wan_backup ] ;then
echo -n " [backup] "
else
echo -n " [primary] "
fi
echo "is alive"
fi
# If we are using wan backup right now we check if primary connection works
if [ -e /tmp/wan_backup ] ;then
if eval $route_via_gw ;then
if eval $ping_command ;then
# Doesn't work
echo "Keep using backup"
ip route del $ping_address via $primary_gw &> /dev/null
else # It works so we change active connection
echo "Primary connection is alive, switching to it"
change_to_primary
rm /tmp/wan_backup
fi
else
echo "Keep using backup 2"
ip route del $ping_address via $primary_gw &> /dev/null
fi
fi
}
while true
do
echo "Run connection keeper..."
connection_checker
sleep $check_delay
done