#!/bin/bash # Purpose: Change Gateway Script if ISP is down # eth0: LAN - 172.17.20.100/16 # eth1: ISP1 - 182.152.162.xx # eth2: ISP2 - 179.158.21.xx # # Change Gateway According to your ISP/Need P_Gateway=182.152.162.xx S_Gateway=179.158.21.xx # Change DNS According to your ISP/Need P_DNS1=8.8.8.8 P_DNS2=8.8.4.4 S_DNS1=8.8.8.8 S_DNS2=8.8.4.4 # Change Network According to your setup MASQUERADE=172.17.0.0/16 Gateway=`/sbin/ip route | grep "default" | awk '{print $3}'` /bin/ping $P_Gateway -w 30 > /dev/null if [ $? -ne 0 ];then echo "Primary gateway $P_Gateway is not Reachable"; /bin/ping $S_Gateway -w 30 > /dev/null if [ $? -ne 1 ];then if [ $Gateway = $S_Gateway ];then echo "Secondary Gateway $S_Gateway Already Added" else /sbin/ip r d default ; /sbin/ip r a default via $S_Gateway dev eth2; echo "Secondary Gateway $S_Gateway Successfully Added" /sbin/iptables -t nat -D POSTROUTING -s $MASQUERADE -o eth1 -j MASQUERADE /sbin/iptables -t nat -A POSTROUTING -s $MASQUERADE -o eth2 -j MASQUERADE #echo "nameserver $S_DNS1" > /etc/resolv.conf #echo "nameserver $S_DNS2" >> /etc/resolv.conf fi; else echo "$P_Gateway and $S_Gateway both are down, Please Contact to ISP" fi; else if [ $Gateway = $P_Gateway ];then echo "Primary Gateway $P_Gateway Already Added" else /sbin/ip r d default ; /sbin/ip r a default via $P_Gateway dev eth1; echo "Primary Gateway $P_Gateway Successfully Added" #echo "nameserver $P_DNS1" > /etc/resolv.conf #echo "nameserver $P_DNS2" >> /etc/resolv.conf /sbin/iptables -t nat -D POSTROUTING -s $MASQUERADE -o eth2 -j MASQUERADE /sbin/iptables -t nat -A POSTROUTING -s $MASQUERADE -o eth1 -j MASQUERADE fi; fi; exit 0;