This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
failover.sh
138 lines (127 loc) · 5.12 KB
/
failover.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
#################################
# Internet Failover Script v2.6 #
# by igroykt #
#################################
#!/bin/sh
PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin"
CHECK_HOST="8.8.8.8"
WAN1=""
WAN1_GW=""
SUBJECT_WAN1="Internet Failover Notify [TTK]"
PING_ERR_WAN1="Ping failed to $CHECK_HOST! Route changed to RTK."
IP_ERR_WAN1="TTK lost IP address! Route changed to RTK."
LINK_ERR_WAN1="TTK link down! Route changed to RTK."
UPLINK_WAN1="TTK is UP! Route changed to TTK."
WAN2=""
WAN2_GW=""
SUBJECT_WAN2="Internet Failover Notify [RTK]"
PING_ERR_WAN2="Ping failed to $CHECK_HOST! Route changed to TTK."
IP_ERR_WAN2="RTK lost IP address! Route changed to TTK."
LINK_ERR_WAN2="RTK link down! Route changed to TTK."
DEFAULT_GW=`netstat -r|grep default|awk '{print $2}'`
check_ping() {
if ! ping -s1 -S $WAN1 -c4 -t4 $CHECK_HOST > /dev/null
then
sleep 4
if ! ping -s1 -S $WAN1 -c4 -t4 $CHECK_HOST > /dev/null
then
if [ "$DEFAULT_GW" != "$WAN2_GW" ]
then
if [ ! -f wan1_link.lost ]; then
if [ ! -f wan1_ip.lost ]; then
route change default $WAN2_GW
php failover_notify.php $SUBJECT_WAN1 $PING_ERR_WAN1
fi
fi
fi
else
if ! ping -s1 -S $WAN2 -c4 -t4 $CHECK_HOST > /dev/null
then
sleep 4
if ! ping -s1 -S $WAN2 -c4 -t4 $CHECK_HOST > /dev/null
then
if [ "$DEFAULT_GW" != "$WAN1_GW" ]
then
if [ ! -f wan2_link.lost ]; then
if [ ! -f wan2_ip.lost ]; then
route change default $WAN1_GW
php failover_notify.php $SUBJECT_WAN2 $PING_ERR_WAN2
fi
fi
fi
fi
fi
fi
fi
if ping -s1 -S $WAN1 -c4 -t4 $CHECK_HOST > /dev/null
then
if [ "$DEFAULT_GW" != "$WAN1_GW" ]
then
route change default $WAN1_GW
php failover_notify.php $SUBJECT_WAN1 $UPLINK_WAN1
fi
fi
}
check_ip() {
wan1_ip=`ifconfig|grep $WAN1|wc -l`
wan2_ip=`ifconfig|grep $WAN2|wc -l`
if [ "$wan1_ip" -ne "1" ]; then
if [ "$DEFAULT_GW" != "$WAN2_GW" ]; then
if [ ! -f wan1_ip.lost ]; then
touch wan1_ip.lost
fi
route change default $WAN2_GW
php failover_notify.php $SUBJECT_WAN1 $IP_ERR_WAN1
fi
else
if [ -f wan1_ip.lost ]; then
rm -f wan1_ip.lost
fi
fi
if [ "$wan2_ip" -ne "1" ]; then
if [ "$DEFAULT_GW" != "$WAN1_GW" ]; then
if [ ! -f wan2_ip.lost ]; then
touch wan2_ip.lost
fi
route change default $WAN1_GW
php failover_notify.php $SUBJECT_WAN2 $IP_ERR_WAN2
fi
else
if [ -f wan2_ip.lost ]; then
rm -f wan2_ip.lost
fi
fi
}
check_link() {
wan1_link=`ifconfig re0|grep active|awk '{print $2}'|wc -l`
wan2_link=`ifconfig ue0|grep active|awk '{print $2}'|wc -l`
if [ "$wan1_link" -ne "1" ]; then
if [ "$DEFAULT_GW" != "$WAN2_GW" ]; then
if [ ! -f wan1_link.lost ]; then
touch wan1_link.lost
fi
route change default $WAN2_GW
php failover_notify.php $SUBJECT_WAN1 $LINK_ERR_WAN1
fi
else
if [ -f wan1_link.lost ]; then
rm -f wan1_link.lost
fi
fi
if [ "$wan2_link" -ne "1" ]; then
if [ "$DEFAULT_GW" != "$wAN1_GW" ]; then
if [ ! -f wan2_link.lost ]; then
touch wan2_link.lost
fi
route change default $wAN1_GW
php failover_notify.php $SUBJECT_WAN2 $LINK_ERR_WAN2
fi
else
if [ -f wan2_link.lost ]; then
rm -f wan2_link.lost
fi
fi
}
check_link
check_ip
check_ping