Created
November 15, 2017 19:49
-
-
Save toast254/6adf8098e898ef5365b1a688f4bdbb64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#********************************************************************* | |
# Configuration | |
#********************************************************************* | |
DEF_GATEWAY="192.168.1.2" # default route | |
BCK_GATEWAY="192.168.1.1" # backup route | |
RMT_IP_1="8.8.8.8" # first remote ip to test | |
RMT_IP_2="8.8.4.4" # second remote ip to test | |
PING_TIMEOUT="15" # ping timeout in seconds | |
SLEEP_TIME="30" # sleep during this time before looping again | |
BYPASS_DEFAULT_GW_FILE="/tmp/use_default_gateway" # if this file exists bypass connectivity tests | |
LOG_FILE="/var/log/default_route_switching.log" # file to write events into | |
#********************************************************************* | |
# check if it's running as root | |
if [ `whoami` != "root" ] | |
then | |
echo "Failover script must be run as root!" | |
exit 1 | |
fi | |
# looping forever | |
while true | |
do | |
# get curently used gateway | |
CURRENT_GW=`ip route show | grep default | awk '{ print $3 }'` | |
# if current gateway is the default one then check directly if it's working | |
if [ "$CURRENT_GW" == "$DEF_GATEWAY" ] | |
then | |
# test connection | |
ping -c 2 -W $PING_TIMEOUT $RMT_IP_1 > /dev/null | |
PING_1=$? | |
ping -c 2 -W $PING_TIMEOUT $RMT_IP_2 > /dev/null | |
PING_2=$? | |
else | |
# else it's currently the backup gateway used, so add route to check if defaut gateway is working | |
# add temporary static routes to remote ip's using default gateway | |
ip route add $RMT_IP_1 via $DEF_GATEWAY | |
ip route add $RMT_IP_2 via $DEF_GATEWAY | |
# test connection | |
ping -c 2 -W $PING_TIMEOUT $RMT_IP_1 > /dev/null | |
PING_1=$? | |
ping -c 2 -W $PING_TIMEOUT $RMT_IP_2 > /dev/null | |
PING_2=$? | |
# delete temporary static route to remote ip's using default gateway | |
ip route del $RMT_IP_1 | |
ip route del $RMT_IP_2 | |
fi | |
# get curent date_time | |
LOG_TIME=`date --rfc-3339=s` | |
# if forcing default gateway | |
if [ -f $BYPASS_DEFAULT_GW_FILE ] | |
then | |
# if the default gateway is not already set | |
if [ "$CURRENT_GW" != "$DEF_GATEWAY" ] | |
then | |
# switching to default gateway | |
ip route del default | |
ip route add default via $DEF_GATEWAY | |
# flushing route cache | |
ip route flush cache | |
echo "$LOG_TIME: forced Gateway to Default with IP $DEF_GATEWAY" >> $LOG_FILE | |
fi | |
#elif [ "$PING_1" == "1" ] || [ "$PING_2" == "1" ] # pessimistic test, at least one test failed so switch to backup | |
elif [ "$PING_1" == "1" ] && [ "$PING_2" == "1" ] # optimistic test, the two tests failed then switch to backup | |
then | |
# if one ping failed then switch to backup gateway | |
# if the backup gateway is not already set | |
if [ "$CURRENT_GW" == "$DEF_GATEWAY" ] | |
then | |
# switch to backup gateway | |
ip route del default | |
ip route add default via $BCK_GATEWAY | |
# flushing routing cache | |
ip route flush cache | |
echo "$LOG_TIME: switched Gateway to Backup with IP $BCK_GATEWAY" >> $LOG_FILE | |
fi | |
elif [ "$CURRENT_GW" != "$DEF_GATEWAY" ] | |
then | |
# switching to default gateway | |
ip route del default | |
ip route add default via $DEF_GATEWAY | |
# flushing route cache | |
ip route flush cache | |
echo "$LOG_TIME: switched Gateway to Default with IP $DEF_GATEWAY" >> $LOG_FILE | |
fi | |
# wait second before looping again | |
sleep $SLEEP_TIME | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@shahidaslam01
this file will "pause" this script and enforce the system to use the default gateway (
DEF_GATEWAY
)having it in the
/tmp
folder give the permissions to user to perform this action.