Load Balancing & Failover Script for Dual/ Multi WAN / ADSL / Cable Connections on Linux with Mail on failure

By Angsuman Chakraborty, Gaea News Network
Wednesday, February 13, 2008

Please read the original article - How To: Load Balancing & Failover With Dual/ Multi WAN / ADSL / Cable Connections on Linux. It contains detailed information with a sample script. Viliam Kočinský modified my script to send email when routing table changes. Also he is starting the script as a service from /etc/init.d. Read below for his enhancements:

I’m using your gwping script. Realy usefull. I added posibility to send email, when routing table change:

# cat /usr/local/sbin/gwping
…….

EMAILFROM="root@myserver.example.com"
EMAILTO="someone@example.com"
…..

if [[ $CLS1 -eq 0 || $CLS2 -eq 0 ]]; then
if [[ $LLS1 -eq 1 && $LLS2 -eq 0 ]]; then
echo Switching to $NAME2
ip route replace default scope global via $GW2 dev $EXTIF2
LSROUTE=$(ip route show)
echo "$LSROUTE" | mail -s "GWPING - Routing table changed. Line $NAME1 is down." -r $EMAILFROM $EMAILTO
elif [[ $LLS1 -eq 0 && $LLS2 -eq 1 ]]; then
echo Switching to $NAME1
ip route replace default scope global via $GW1 dev $EXTIF1
LSROUTE=$(ip route show)
echo "$LSROUTE" | mail -s "GWPING - Routing table changed. Line $NAME2 is down." -r $EMAILFROM $EMAILTO
elif [[ $LLS1 -eq 0 && $LLS2 -eq 0 ]]; then
echo Restoring default load balancing
#ip route replace default scope global via $GW1 dev $EXTIF1
ip route replace default scope global nexthop via $GW1 dev $EXTIF1 weight $W1 nexthop via $GW2 dev $EXTIF2 weight $W2
LSROUTE=$(ip route show)
echo "$LSROUTE" | mail -s "GWPING - Routing table changed. Both lines are up." -r $EMAILFROM $EMAILTO
fi
fi
sleep $SLEEPTIME
done

Also I’m starting this script as service from /etc/init.d

# cat /etc/init.d/gwping
#!/bin/sh
# Author: Viliam Kocinsky

### BEGIN INIT INFO
# Short-Description: Monitor Internet access connection links and modify kernel route if some link go down.
# Description: This is startup script for gwping script, that manage internet connection failover (if you have at least 2 providers).
# That is, it reguraly ping one IP address
# on internet through certain provider (but trying all) . If ping doesn’t come back, than it addjust kernel route table.
# See inside script for details. This script is from the website:
# https://blog.taragana.com/index.php/archive/how-to-load-balancing-failover-with-dual-multi-wan-adsl-cable-connections-on-linux/
#
### END INIT INFO

command="$1"

case "$command" in
start)
echo "Starting gwping."
/usr/local/sbin/gwping &
echo $! > /usr/local/share/gwping_pid
;;
stop)
echo "Stoping gwping."
kill -9 $(cat /usr/local/share/gwping_pid) && rm /usr/local/share/gwping_pid
;;
*)
echo "Usage /etc/init.d/gwping start|stop"
;;
esac

Discussion

Gabriel
May 10, 2010: 7:49 am

Your script is great, i got it working perfectly for normal sites and smtp/pop traffic.
The only problem that I’m having is with https and some bank sites. The connection starts ok, but when I navigate to the second or third page I got disconnected, something related to the checks my bank do regarding my session. How can I handle this?


Ajay Bakshi BSc
September 15, 2008: 2:23 am

hello, i have researched xrio products and they do wan load balancing. but this is only a generic routing script for distributing wan data through internet connections. thanks.


martin hill
September 12, 2008: 4:12 am

is this for xrio ubm appliance?

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :