tag:blogger.com,1999:blog-1135701757802805375.post313403246063885933..comments2023-05-26T09:44:59.815+02:00Comments on Working with Linux: Multiple gateways on the same hostpela-suroshttp://www.blogger.com/profile/07522894660925331288noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-1135701757802805375.post-48993667207497972682013-09-10T08:38:06.980+02:002013-09-10T08:38:06.980+02:00Thanks for posting. just looking RSS Feeds.Thanks for posting. just looking RSS Feeds.Pramod Kumar Pandeyhttps://www.blogger.com/profile/18334641743922808539noreply@blogger.comtag:blogger.com,1999:blog-1135701757802805375.post-87111826929006831622012-01-04T09:11:12.852+01:002012-01-04T09:11:12.852+01:00I am glad the post was useful, Saman.
Thanks to y...I am glad the post was useful, Saman.<br /><br />Thanks to you as well for contributing with the script.pela-suroshttps://www.blogger.com/profile/07522894660925331288noreply@blogger.comtag:blogger.com,1999:blog-1135701757802805375.post-6692678201921388872012-01-04T02:47:36.023+01:002012-01-04T02:47:36.023+01:00Thanks you for this very useful post,
You saved my...Thanks you for this very useful post,<br />You saved my day. I was writing a daemon that changes the default gateway by testing the FUNCTIONALITY of multiple gateways ... and with your setup I can now benchmark multiple gateways and prefer the best (fastest)!<br />Here is my script:<br />###############################<br />#!/bin/bash<br /><br /><br />GW1="192.168.10.100"<br />GW2="192.168.10.200"<br />OUR_GWS="$GW1 $GW2"<br />IFACE1_DEV="eth0"<br />COUNTER=0<br />FAILCOUNT_LIMIT=2<br /><br /><br /># wee need ad traceroute or routine for GW priorising!<br /># ping -I 192.168.100.221 google.com<br /># ping -I 192.168.100.222 google.com<br />#<br /><br /><br />while true ; do<br /> # Get gateway<br /> DEFAULT_GW=$(ip r |grep "^default via"|awk '{print $3}')<br /> #<br /> # If no default gateway is set, then<br /> # delete all foreign routes and<br /> # set GW1 as default.<br /> if [ -z $DEFAULT_GW ] ; then<br /> for i in $(ip r|egrep -v ''"$GW1"'|'"$GW2"''|grep "dev[[:space:]]*eth0[[:space:]]*scope[[:space:]]*link[[:space:]]*$"|awk '{print $1}'); do<br /> ip r d $i dev eth0 scope link<br /> done<br /> ip r a default via $GW1<br /> fi<br /> #<br /> # Check if we use OUR_GWS, and if not then,<br /> # delete all foreign routes,<br /> # add our routes to routing table and<br /> # set GW1 as default if possible, else set GW2 as default<br /> if ! echo "$OUR_GWS"|grep -q "$DEFAULT_GW" ; then<br /> for i in $(ip r|egrep -v ''"$GW1"'|'"$GW2"''|grep "dev[[:space:]]*eth0[[:space:]]*scope[[:space:]]*link[[:space:]]*$"|awk '{print $1}'); do<br /> ip r d $i dev eth0 scope link<br /> done<br /> ip r d $(ip r|grep "^default via")<br /> ip r a $GW1 dev eth0<br /> ip r a $GW2 dev eth0<br /> ip r chg default via $GW1 || ip r chg default via $GW2<br /> fi<br /> #<br /> # Check if we succeeded setting the GW<br /> DEFAULT_GW=$(ip r |grep "^default via"|awk '{print $3}')<br /> if ! echo "$OUR_GWS"|grep -q $DEFAULT_GW ; then<br /> echo "ERROR: Could not set any GW!"<br /> fi<br /> #<br /> # Check if our GW is routing properly.<br /> if ping -W 2 -w 1 -c 1 google.com > /dev/null 2>&1 ; then<br /> COUNTER=0<br /> sleep 30<br /> else<br /> sleep 1<br /> let COUNTER++<br /> fi<br /> #<br /> # If routing fails, then switch GW.<br /> if [ $COUNTER -ge $FAILCOUNT_LIMIT ] ; then<br /> FAILED_GW=$(ip r|grep "^default via"|awk '{print $3}')<br /> if [ "$FAILED_GW" == "$GW1" ] ; then<br /> HEALTHY_GW=$GW2<br /> else<br /> HEALTHY_GW=$GW1<br /> fi<br /> ip r a $HEALTHY_GW dev eth0<br /> ip r chg default via $HEALTHY_GW<br /> fi<br /> #<br /> sleep 1<br />done<br />###############################Saman Behnamhttps://www.blogger.com/profile/06526410257231773278noreply@blogger.com