-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiwan.sh
executable file
·136 lines (111 loc) · 3.08 KB
/
multiwan.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
#!/usr/bin/env bash
source /etc/multiwan.conf
STARTUP=1
CHECK_INTERVAL=1
# IP address of each WAN interface
WAN_NET1="$(ip addr show $WAN_IF1 | grep "inet " | tr -s [:space:] | cut -d ' ' -f3)"
WAN_NET2="$(ip addr show $WAN_IF2 | grep "inet " | tr -s [:space:] | cut -d ' ' -f3)"
WAN_IP1="$(echo $WAN_NET1 | cut -d '/' -f1)"
WAN_IP2="$(echo $WAN_NET2 | cut -d '/' -f1)"
# Last link status. Defaults to down to force check of both on first run.
LLS1=1
LLS2=1
# Last ping status.
LPS1=1
LPS2=1
# Current ping status.
CPS1=1
CPS2=1
# Change link status.
CLS1=1
CLS2=1
# Count of consecutive status checks
COUNT1=0
COUNT2=0
function link_status() {
case $1 in
0)
echo "Up" ;;
1)
echo "Down" ;;
*)
echo "Unknown" ;;
esac
}
# check_link $IP $TIMEOUT
function check_link() {
ping -W $2 -I $1 -c 1 $PING_TARGET > /dev/null 2>&1
RETVAL=$?
if [ $RETVAL -ne 0 ] ; then
STATE=1
else
STATE=0
fi
link_status $STATE
return $STATE
}
while : ; do
LINK_STATE="$(check_link $WAN_IP1 $PING_TIMEOUT)"
CPS1=$?
if [ $LPS1 -ne $CPS1 ] ; then
logger -p local6.notice -t MULTIWAN[$$] "Ping state changed for $WAN_TABLE1 from $(link_status $LPS1) to $(link_status $CPS1)"
COUNT1=1
else
if [ $LPS1 -ne $LLS1 ] ; then
COUNT1=`expr $COUNT1 + 1`
fi
fi
if [[ $COUNT1 -ge $SUCCESS_COUNT || ($LLS1 -eq 0 && $COUNT1 -ge $FAILURE_COUNT) ]]; then
CLS1=0
COUNT1=0
if [ $LLS1 -eq 1 ] ; then
LLS1=0
else
LLS1=1
fi
logger -p local6.notice -t MULTIWAN[$$] "Link state for $WAN_TABLE1 is $(link_status $LLS1)"
else
CLS1=1
fi
LPS1=$CPS1
LINK_STATE="$(check_link $WAN_IP2 $PING_TIMEOUT)"
CPS2=$?
if [ $LPS2 -ne $CPS2 ] ; then
logger -p local6.notice -t MULTIWAN[$$] "Ping state changed for $WAN_TABLE2 from $(link_status $LPS2) to $(link_status $CPS2)"
COUNT2=1
else
if [ $LPS2 -ne $LLS2 ] ; then
COUNT2=`expr $COUNT2 + 1`
fi
fi
if [[ $COUNT2 -ge $SUCCESS_COUNT || ($LLS2 -eq 0 && $COUNT2 -ge $FAILURE_COUNT) ]]; then
CLS2=0
COUNT2=0
if [ $LLS2 -eq 1 ]; then
LLS2=0
else
LLS2=1
fi
logger -p local6.notice -t MULTIWAN[$$] "Link state for $WAN_TABLE2 is $(link_status $LLS2)"
else
CLS2=1
fi
LPS2=$CPS2
if [[ $CLS1 -eq 0 || $CLS2 -eq 0 ]] ; then
if [[ $STARTUP -eq 1 ]] ; then
STARTUP=0
CHECK_INTERVAL=15
fi
if [[ $LLS1 -eq 1 && $LLS2 -eq 0 ]] ; then
logger -p local6.notice -t MULTIWAN[$$] "Applying $WAN_TABLE2 only route."
ip route change default scope global via $WAN_GW2 dev $WAN_IF2
elif [[ $LLS1 -eq 0 && $LLS2 -eq 1 ]] ; then
logger -p local6.notice -t MULTIWAN[$$] "Applying $WAN_TABLE1 only route."
ip route change default scope global via $WAN_GW1 dev $WAN_IF1
elif [[ $LLS1 -eq 0 && $LLS2 -eq 0 ]] ; then
logger -p local6.notice -t MULTIWAN[$$] "Applying multiwan load balancing route."
ip route replace default scope global nexthop via $WAN_GW1 dev $WAN_IF1 weight $WAN_WEIGHT1 nexthop via $WAN_GW2 dev $WAN_IF2 weight $WAN_WEIGHT2
fi
fi
sleep $CHECK_INTERVAL
done