-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfogw.sh
80 lines (78 loc) · 3.07 KB
/
fogw.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
#!/bin/bash
#Version: 1.1.6
#https://github.com/IBeholderI/FailoverGateway/blob/main/fogw.sh
#*********************************************************************
# Configuration
#*********************************************************************
DEF_GW="$1" # Default Gateway
BCK_GW="$2" # Backup Gateway
PING_TMO="$3" # Ping timeout in milliseconds
AUTO_FB="$4" # Auto failback on/off
#*********************************************************************
#Check GW
CURT_GW=$(ip route show | grep default | awk '{ print $3 }')
if [ -z "${CURT_GW}" ]
then
ip route add default via "$DEF_GW"
echo "Default gateway with IP \"$DEF_GW\" was set"
fi
if [ "$CURT_GW" == "$DEF_GW" ]
then
if ! fping -c 2 -t "$PING_TMO" "$DEF_GW" &> /dev/null
then
if fping -c 2 -t "$PING_TMO" "$BCK_GW" &> /dev/null
then
# switching to backup
ip route del default
ip route add default via "$BCK_GW"
echo "Gateway switched to backup with IP \"$BCK_GW\""
exit 0
else
echo "No gateways are reachable"
exit 1
fi
fi
elif [ "$CURT_GW" == "$BCK_GW" ]
then
case $AUTO_FB in
# With automatic failback to default gateway when it comes up
on)
fping -c 2 -t "$PING_TMO" "$BCK_GW" &> /dev/null
PING_BCK_GW=$?
fping -c 2 -t "$PING_TMO" "$DEF_GW" &> /dev/null
PING_DEF_GW=$?
if [ "$PING_DEF_GW" == "0" ]
then
# switching to default
ip route del default
ip route add default via "$DEF_GW"
echo "Gateway switched to default with IP \"$DEF_GW\""
exit 0
elif [ "$PING_BCK_GW" != "0" ]
then
echo "No gateways are reachable"
exit 1
fi
;;
# Without automatic failback to default gateway
off)
if ! fping -c 2 -t "$PING_TMO" "$BCK_GW" &> /dev/null
then
if fping -c 2 -t "$PING_TMO" "$DEF_GW" &> /dev/null
then
# Switching to default
ip route del default
ip route add default via "$DEF_GW"
echo "Gateway switched to Default with IP \"$DEF_GW\""
exit 0
else
echo "No gateways are reachable"
exit 1
fi
fi
;;
esac
else
echo "fogw configured gateways are different then currently used. Current default gateway must be empty or the same as default or backup gateway placed in fogw config or maybe fogw gateways config is wrong."
exit 1
fi