Bash Script to check and shift ISP if unreachable || linuxtopic
vi /usr/local/bin/shift_gateway
#!/bin/bash# by lokesh carpenter ## Change Gateway According to your ISP/NeedP_Gateway=182.152.162.xxS_Gateway=179.158.21.xx# Change DNS According to your ISP/NeedP_DNS1=8.8.8.8P_DNS2=8.8.4.4S_DNS1=8.8.8.8S_DNS2=8.8.4.4# Change Network According to your setupMASQUERADE=172.17.0.0/16Gateway=`/sbin/ip route | grep "default" | awk '{print $3}'`/bin/ping $P_Gateway -w 30 > /dev/nullif [ $? -ne 0 ];thenecho "Primary gateway $P_Gateway is not Reachable";/bin/ping $S_Gateway -w 30 > /dev/nullif [ $? -ne 1 ];thenif [ $Gateway = $S_Gateway ];thenecho "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.conffi;elseecho "$P_Gateway and $S_Gateway both are down, Please Contact to ISP"fi;elseif [ $Gateway = $P_Gateway ];thenecho "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 MASQUERADEfi;fi;exit 0;
Explanation :
Gateway=`/sbin/ip route | grep "default" | awk '{print $3}'` : checking current system gateway,
/bin/ping $P_Gateway -w 30 : ping primary gateway for 30 seconds
if [ $? -ne 0 ]; : if response is 0 then
/bin/ping $S_Gateway -w 30 : ping secondary gateway
if [ $? -ne 1 ]; : set secondary gateway otherwise echo both gateway are down.
if [ $Gateway = $S_Gateway ];then :check secondary gateway, if already found echo “Already Set S_Gateway, otherwise set Secondary Gateway
/sbin/iptables -t nat -D POSTROUTING -s $MASQUERADE -o eth1 -j MASQUERADE delete nat rules for eth1
/sbin/iptables -t nat -A POSTROUTING -s $MASQUERADE -o eth2 -j MASQUERADE
Set nat rules for eth2
#echo "nameserver $S_DNS1" > /etc/resolv.conf
#echo "nameserver $S_DNS2" >> /etc/resolv.conf : change dns entry as per ISP , here i comment because i used 8.8.8.8 & 8.8.4.4
else
if [ $Gateway = $P_Gateway ] echo "Primary Gateway $P_Gateway Already Added"
else set $P_Gateway on dev eth1;
#echo "nameserver $P_DNS1" > /etc/resolv.conf
#echo "nameserver $P_DNS2" >> /etc/resolv.conf
change dns entry as per ISP , here i comment because i used 8.8.8.8 & 8.8.4.4
/sbin/iptables -t nat -D POSTROUTING -s $MASQUERADE -o eth2 -j MASQUERADE delete nat rules for eth2
/sbin/iptables -t nat -A POSTROUTING -s $MASQUERADE -o eth1 -j MASQUERADE Set nat rules for eth1
Set Execution Permission
chmod +x /usr/local/bin/shift_gateway
Set Cron for Checking Gateway in every minutes
vi /etc/cron.d/gateway_check
* * * * * /bin/bash /usr/local/bin/shift_gateway >> /var/log/shift_gateway-`date -I`.log
Leave a Comment