Bash script to check internet connection, This is a script to change ISP if it’s unreachable, i have 2 ISP and 3 interface eth0 eth1 and eth2, My eth0 is connected on Intranet and ISP connected to eth1 and eth2. This script ping my Primary ISP if its reachable exit if it’s not reachable set secondary isp
vi /usr/local/bin/shift_gateway
#!/bin/bash
# by lokesh carpenter #
# 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;
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