Categories: MiscTechnology

Dual WAN (gateway) choosing the best connection first

#!/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'
Santi Strati

Share
Published by
Santi Strati

Recent Posts

OPENWRT e DD-WRT – Alternativa open e free a Cisco IOS

OPENWRT e DD-WRT - Alternativa open e free a Cisco IOS Scarica il file PDF

4 anni ago

EasyList Italy + EasyList AdBlock

EasyList Italy+EasyList Add these following lines to adblock.sources.gz (located on /etc/adblock/): [code] "reg_it": { "url":…

4 anni ago

WGET auto resume script for dropping connection

[code light="true" language="bash"] LINK='https://software-download.microsoft.com/db/Win10_2004_Italian_x64.iso' SLEEPTIME='120' while true do if wget -c ${LINK}; then echo 'Download…

4 anni ago

Voting Machine Smartmatic A4-210…

... come riconvertirlo ad uso didattico, ludico, ufficio o punto chiosco. Come molti di voi…

5 anni ago

Retropie es_input.cfg Logitech Wireless Gamepad F710 and Keyboard

[code light="true" language="xml"] <?xml version="1.0"?> <inputList> <inputConfig type="joystick" deviceName="Logitech Gamepad F710" deviceGUID="030000006d0400001fc2000005030000"> <input name="a" type="button"…

5 anni ago