Failover script for Connection back up
Hi this is a script for backing up the internet connection on a debian server...still buggy and testing it and not finished but someone might use it..
If you want to contact me you can do so on my blog The VPN Guru - http://thevpn.guru
Assumptions:
1-pppoe always on connection "should be used as long as up"
2-cable limited backup connection
3-pppoe can not ping external IPs "ISP settings :S" so to test the connection either download a small file or resolve a hostname
4-If pppoe fails 3 times in a row switch to cable...when pppoe up again switch to it
5-pppoe and cable have different firewall setups ....these setups are stored in different firewall scripts which are executed depending on the active connection...
#!/bin/bash
wise="eth2" # wisebox connectedhere
rabbit="true" #assume rabbit is a up as a start condition
count=0 #initialise count to 0
#since the wise box connection is obsolete stop all traffic on it
iptables -I INPUT -i $wise -j DROP
iptables -I FORWARD -i $wise -j DROP
#iptables -I OUTPUT -i $wise -j DROP
#create a while loop to test for rabbit overall conditions
while [ $rabbit = "true" ]; do
#clear variable values each time
eth0="" # rabbit connected here
ppp0="" # pppoe link
conn="" # pppoe connectivity
conn2="" #second test of pppoe connectivity
#Test for eth0 link
eth0=`/sbin/ifconfig -a | grep "eth0"`
#note that in the if statement you have to enclose the variable in double quotes because the line repeated by $eth0 contains whitespaces
if [ -n "$eth0" ];then # -n tests to see if the argument is non empty
echo "eth0 is up"
else
echo "eth0 is down"
echo -e " `date` NOCCBOX says eth0 is down" >> /var/log/syslog
rabbit="false"
fi
#Test for pppoe link
ppp0=`/sbin/ifconfig -a | grep "ppp0"`
if [ -n "$ppp0" ]; then # -n tests to see if the argument is non empty
echo "ppp0 is up"
else
echo "ppp0 is down"
echo -e " `date` NOCCBOX says ppp0 is down" >> /var/log/syslog
rabbit="false"
fi
#Test for pppoe connectivity
conn=`ping -c 1 -W 3 www.nocclb.com | grep "65.75.128.46"`
if [ -n "$conn" ]; then # -n tests to see if the argument is non empty
echo "conn is up"
count=0 #reset counter
else
conn2=`ping -c 1 -W 3 www.google.com | grep "66.102.9"` #dont insert the last octet since google has multiple IP which differ according to last octet
if [ -n "$conn2" ]; then
echo "conn is up in second test"
count=0 #reset counter
else
echo "conn is down"
echo -e " `date` NOCCBOX says conn is down" >> /var/log/syslog
#Increment count by 1
count=$((count + 1))
if[ $((count = 3)) ]; then #after three consecutive fails set rabbit false
rabbit="false"
fi
sleep 60 #sleep for one minute before retrying
done #end of while loop
#now that rabbit failed the connection should switch to the wisebox cable connection
echo -e " `date` switching to wisebox connection" >> /var/log/syslog
#clear the routing table
#fetch dns info and default gw
dhclient $wise
#call the wisebox firewall script
#sh nocc.cable.wise.sh
while [ $rabbit = "false" ]; do
eth0=`/sbin/ifconfig -a | grep "eth0"`
if [ -n "$eth0" ];then # -n tests to see if the argument is non empty
echo "eth0 is up while in wisebox"
#If eth0 up test for pppoe link
ppp0=`/sbin/ifconfig -a | grep "ppp0"`
if [ -n "$ppp0" ]; then # -n tests to see if the argument is non empty
echo "ppp0 is up while in wisebox"
#Test for pppoe connectivity since both ppp0 and eth0 are up
#get the ip address of the gateway of the pppoe connection
gateway=`ifconfig ppp0 | grep "inet addr:" | awk -F : '{print$2}'| awk '{print$1}'`
#get the ip address of the pppoe for wget
ppp0_address=`ifconfig ppp0 | grep "inet addr:" | awk -F : '{print$2}' | awk '{print$1}'`
#add entry into routing table to allow to ping through pppo if it is up to check for connectivity
route add $gateway dev ppp0
#flush dns cache
service named stop
service named start
ifconfig eth2 down #stop wise.....testing here only might be totally wrong
#conn=`ping -I ppp0 -c 1 -W 3 www.nocclb.com | grep "65.75.128.46"`
#check if a file can be downloaded by binding the address of ppp0 to the download command
ppp0_test=`wget --bind-address=$ppp0_address http://bihnet.linux.tucows.com/files/snes9express-1.26-2.src.rpm`
if [ -n "$conn" ]; then # -n tests to see if the argument is non empty
echo "conn is up"
else
conn2=`ping -c1 -W 3 www.google.com | grep "66.102.9"` #dont insert the last octet since google has multiple IP which differ according to last octet
if [ -n "$conn2" ]; then
echo "conn is up "
echo -e " `date` conn is up" >> /var/log/syslog
rabbit="true"
fi
fi
sleep 60
done
#Now that rabbit evaluated true......the routing table has to be reset again and the dns reset and pppoe firewall script has to be executed
#after that the script calls it self again and executes..this guarantees that the script will always be running
#fetch dns info and default gw
dhclient eth0
#reset routing table
#call firewall script
# sh firwall.pppoe.script.sh
#call this script
chmod u+x fail.sh
sh fail.sh
If you want to contact me you can do so on my blog The VPN Guru - http://thevpn.guru
Assumptions:
1-pppoe always on connection "should be used as long as up"
2-cable limited backup connection
3-pppoe can not ping external IPs "ISP settings :S" so to test the connection either download a small file or resolve a hostname
4-If pppoe fails 3 times in a row switch to cable...when pppoe up again switch to it
5-pppoe and cable have different firewall setups ....these setups are stored in different firewall scripts which are executed depending on the active connection...
#!/bin/bash
wise="eth2" # wisebox connectedhere
rabbit="true" #assume rabbit is a up as a start condition
count=0 #initialise count to 0
#since the wise box connection is obsolete stop all traffic on it
iptables -I INPUT -i $wise -j DROP
iptables -I FORWARD -i $wise -j DROP
#iptables -I OUTPUT -i $wise -j DROP
#create a while loop to test for rabbit overall conditions
while [ $rabbit = "true" ]; do
#clear variable values each time
eth0="" # rabbit connected here
ppp0="" # pppoe link
conn="" # pppoe connectivity
conn2="" #second test of pppoe connectivity
#Test for eth0 link
eth0=`/sbin/ifconfig -a | grep "eth0"`
#note that in the if statement you have to enclose the variable in double quotes because the line repeated by $eth0 contains whitespaces
if [ -n "$eth0" ];then # -n tests to see if the argument is non empty
echo "eth0 is up"
else
echo "eth0 is down"
echo -e " `date` NOCCBOX says eth0 is down" >> /var/log/syslog
rabbit="false"
fi
#Test for pppoe link
ppp0=`/sbin/ifconfig -a | grep "ppp0"`
if [ -n "$ppp0" ]; then # -n tests to see if the argument is non empty
echo "ppp0 is up"
else
echo "ppp0 is down"
echo -e " `date` NOCCBOX says ppp0 is down" >> /var/log/syslog
rabbit="false"
fi
#Test for pppoe connectivity
conn=`ping -c 1 -W 3 www.nocclb.com | grep "65.75.128.46"`
if [ -n "$conn" ]; then # -n tests to see if the argument is non empty
echo "conn is up"
count=0 #reset counter
else
conn2=`ping -c 1 -W 3 www.google.com | grep "66.102.9"` #dont insert the last octet since google has multiple IP which differ according to last octet
if [ -n "$conn2" ]; then
echo "conn is up in second test"
count=0 #reset counter
else
echo "conn is down"
echo -e " `date` NOCCBOX says conn is down" >> /var/log/syslog
#Increment count by 1
count=$((count + 1))
if[ $((count = 3)) ]; then #after three consecutive fails set rabbit false
rabbit="false"
fi
sleep 60 #sleep for one minute before retrying
done #end of while loop
#now that rabbit failed the connection should switch to the wisebox cable connection
echo -e " `date` switching to wisebox connection" >> /var/log/syslog
#clear the routing table
#fetch dns info and default gw
dhclient $wise
#call the wisebox firewall script
#sh nocc.cable.wise.sh
while [ $rabbit = "false" ]; do
eth0=`/sbin/ifconfig -a | grep "eth0"`
if [ -n "$eth0" ];then # -n tests to see if the argument is non empty
echo "eth0 is up while in wisebox"
#If eth0 up test for pppoe link
ppp0=`/sbin/ifconfig -a | grep "ppp0"`
if [ -n "$ppp0" ]; then # -n tests to see if the argument is non empty
echo "ppp0 is up while in wisebox"
#Test for pppoe connectivity since both ppp0 and eth0 are up
#get the ip address of the gateway of the pppoe connection
gateway=`ifconfig ppp0 | grep "inet addr:" | awk -F : '{print$2}'| awk '{print$1}'`
#get the ip address of the pppoe for wget
ppp0_address=`ifconfig ppp0 | grep "inet addr:" | awk -F : '{print$2}' | awk '{print$1}'`
#add entry into routing table to allow to ping through pppo if it is up to check for connectivity
route add $gateway dev ppp0
#flush dns cache
service named stop
service named start
ifconfig eth2 down #stop wise.....testing here only might be totally wrong
#conn=`ping -I ppp0 -c 1 -W 3 www.nocclb.com | grep "65.75.128.46"`
#check if a file can be downloaded by binding the address of ppp0 to the download command
ppp0_test=`wget --bind-address=$ppp0_address http://bihnet.linux.tucows.com/files/snes9express-1.26-2.src.rpm`
if [ -n "$conn" ]; then # -n tests to see if the argument is non empty
echo "conn is up"
else
conn2=`ping -c1 -W 3 www.google.com | grep "66.102.9"` #dont insert the last octet since google has multiple IP which differ according to last octet
if [ -n "$conn2" ]; then
echo "conn is up "
echo -e " `date` conn is up" >> /var/log/syslog
rabbit="true"
fi
fi
sleep 60
done
#Now that rabbit evaluated true......the routing table has to be reset again and the dns reset and pppoe firewall script has to be executed
#after that the script calls it self again and executes..this guarantees that the script will always be running
#fetch dns info and default gw
dhclient eth0
#reset routing table
#call firewall script
# sh firwall.pppoe.script.sh
#call this script
chmod u+x fail.sh
sh fail.sh