#!/bin/bash # Frequenza in secondi con cui fare il check delle connessioni SLEEPTIME=10 # IP o host esterno da pingare per effettuare le prove TESTIP=www.google.com # Timeout del ping in secondi TIMEOUT=2 # Interfacce EXTIF1=eth1 EXTIF2=eth2 # Indirizzi IP delle interfacce IP1=192.168.1.7 IP2=192.168.0.7 # Gateway delle interfacce GW1=192.168.1.1 GW2=192.168.0.1 # Weights del router. Nella mia guida erano entrami ad 1 per fare il round robin delle connessioni: # Guarda questa riga: ip route add default scope global nexthop via $P1 dev $IF1 weight 1 nexthop via $P2 dev $IF2 weight 1 # Presente nell'articolo http://ideafactory.it/debian/iproute-load-balance-verso-2-gateway.html W1=1 W2=1 # Nome dei provider utilizzati NAME1=ngi NAME2=fastweb # Numero di ripetizioni di successo o di fallimento prima di cambiare lo stato alla connessione SUCCESSREPEATCOUNT=4 FAILUREREPEATCOUNT=1 ### NON MODIFICARE DA QUI IN GIU' # Ultimo stato del link di connessione. NON MODIFICARE! LLS1=1 LLS2=1 # Esito dell'ultimo ping. NON MODIFICARE! LPS1=1 LPS2=1 # Esito del ping corrente. NON MODIFICARE! CPS1=1 CPS2=1 # Variabili che mi servono per sapere se devo cambiare il routing. NON MODIFICARE! CLS1=1 CLS2=1 # Contatori. NON MODIFICARE! COUNT1=0 COUNT2=0 while : ; do ping -W $TIMEOUT -I $IP1 -c 1 $TESTIP > /dev/null 2>&1 RETVAL=$? if [ $RETVAL -ne 0 ]; then echo $NAME1 Down CPS1=1 else CPS1=0 fi if [ $LPS1 -ne $CPS1 ]; then echo Ping status changed for $NAME1 from $LPS1 to $CPS1 COUNT1=1 else if [ $LPS1 -ne $LLS1 ]; then COUNT1=`expr $COUNT1 + 1` fi fi if [[ $COUNT1 -ge $SUCCESSREPEATCOUNT || ($LLS1 -eq 0 && $COUNT1 -ge $FAILUREREPEATCOUNT) ]]; then echo Uptime status will be changed for $NAME1 from $LLS1 CLS1=0 COUNT1=0 if [ $LLS1 -eq 1 ]; then LLS1=0 else LLS1=1 fi else CLS1=1 fi LPS1=$CPS1 ping -W $TIMEOUT -I $IP2 -c 1 $TESTIP > /dev/null 2>&1 RETVAL=$? if [ $RETVAL -ne 0 ]; then echo $NAME2 Down CPS2=1 else CPS2=0 fi if [ $LPS2 -ne $CPS2 ]; then echo Ping status changed for $NAME2 from $LPS2 to $CPS2 COUNT2=1 else if [ $LPS2 -ne $LLS2 ]; then COUNT2=`expr $COUNT2 + 1` fi fi if [[ $COUNT2 -ge $SUCCESSREPEATCOUNT || ($LLS2 -eq 0 && $COUNT2 -ge $FAILUREREPEATCOUNT) ]]; then echo Uptime status will be changed for $NAME2 from $LLS2 CLS2=0 COUNT2=0 if [ $LLS2 -eq 1 ]; then LLS2=0 else LLS2=1 fi else CLS2=1 fi LPS2=$CPS2 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 elif [[ $LLS1 -eq 0 && $LLS2 -eq 1 ]]; then echo Switching to $NAME1 ip route replace default scope global via $GW1 dev $EXTIF1 elif [[ $LLS1 -eq 0 && $LLS2 -eq 0 ]]; then echo Restoring default load balancing ip route replace default scope global nexthop via $GW1 dev $EXTIF1 weight $W1 nexthop via $GW2 dev $EXTIF2 weight $W2 fi fi sleep $SLEEPTIME done