Dual WAN (gateway) choosing the best connection first

dual gateway
#!/bin/sh

INTERVAL='60'
SLEEPTIME='60'
GW1='192.168.1.254'
GW2='192.168.2.2'
PINGENDPOINT='8.8.8.8'
PINGTIME='1'
DEBUG='y'

while true
        do
        route del default gw $GW1
        route add default gw $GW2
        state='2'
        i='1'
        while [ $i -lt $INTERVAL ]
                do
                ping -c $PINGTIME $PINGENDPOINT > /dev/null
                if [ $? -ne '0' ]
                        then
                        if [ $state -eq = '1']
                                then
                                route del default gw $GW1
                                route add default gw $GW2
                                state='1'
                        else
                        route del default gw $GW2
                        route add default gw $GW1
                        state='1'
                        fi
                fi
                if [ $DEBUG == 'y' ]
                    then
                    echo -n "$i - "
                    date
                    route -n | tail -n 1
                fi
                sleep $SLEEPTIME
                i=$((i+1))
        done
done
3 - Sat May 24 11:53:20 UTC 2014
0.0.0.0         192.168.2.2     0.0.0.0         UG    0      0        0 br0

4 - Sat May 24 11:54:20 UTC 2014
0.0.0.0         192.168.1.254   0.0.0.0         UG    0      0        0 eth1
ssh root@beep 'GW1=192.168.1.254;GW2=192.168.2.2;GW=`route -n | tail -n 1 | cut -c 17-29`;if [ ${GW} == ${GW1} ]; then route del default gw ${GW1} && route add default gw ${GW2}; echo ${GW2}; else route del default gw ${GW2} && route add default gw ${GW1}; echo ${GW1}; fi'

Share This

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *