Você está na página 1de 6

#!/bin/bash -x IPTABLES=/sbin/iptables # Convencionalmente " 0 " indica sucesso neste script.

# Tempo entre verificaes em segundos SLEEPTIME=5 # Endereo IP ou nome de domnio para o ping. O roteiro baseia-se no domnio a ser pin gvel e sempre disponvel. TESTIP=200.98.249.120 # Ping Timeout em segundos. TIMEOUT=3 # Interfaces externas. EXTIF1=eth1 EXTIF2=eth2 EXTIF3=eth3 # Endereo IP de interfaces externas. Este no o endereo do gateway. IP1=192.168.1.10 IP2=192.168.2.10 IP3=192.168.3.10 # Gateway endereos IP. Este o primeiro (hop) gateway, poderia ser o seu router IP endereo, se tiver sido configurado como o gateway. GW1=192.168.1.1 GW2=192.168.2.1 GW3=192.168.3.1 # Pesos relativos de rotas. Guarde este para um baixo valor inteiro. Estou usand o 1 para GVT porque 3 vezes mais rpida. W1=1 W2=1 W3=1 # Nome dos Provedores; utilizar os seus prprios nomes aqui. NAME1=gvt NAME2=brt NAME3=net # N de repeties de sucesso ou fracasso antes de mudar status de conexo. SUCCESSREPEATCOUNT=1 FAILUREREPEATCOUNT=4 ## No mudar nada abaixo desta linha. # ltima link status indica que a macro estado da ligao determinada. Trata-se inicia lmente em vigor estabelece encaminhamento mudana adiantado. No alterar estes valor es. LLS1=1 LLS2=1 LLS3=1 # ltima ping estado. No alterar estes valores. LPS1=1 LPS2=1 LPS3=1

# Ping atual estatuto. No alterar estes valores. CPS1=1 CPS2=1 CPS3=1 # Mudana link status indica que a ligao tem de ser mudado. No alterar estes valores. CLS1=1 CLS2=1 CLS3=1 # Conde de repetidos at estatuto ou para baixo estatuto. No alterar estes valores. COUNT1=0 COUNT2=0 COUNT3=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 `date +%x" "%X` Ping status changed for $NAME1 from $LPS1 t o $CPS1 COUNT1=1 else if [ $LPS1 -ne $LLS1 ]; then COUNT1=`expr $COUNT1 + 1` fi fi if [[ $COUNT1 -ge $SUCCESSREPEATCOUNT || ($LLS1 -eq 0 && $COUNT1 -ge $FA ILUREREPEATCOUNT) ]]; then echo `date +%x" "%X` Uptime status will be changed for $NAME1 fr om $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 `date +%x" "%X` $NAME2 Down CPS2=1 else CPS2=0 fi if [ $LPS2 -ne $CPS2 ]; then echo `date +%x" "%X` Ping status changed for $NAME2 from $LPS2 t o $CPS2 COUNT2=1 else if [ $LPS2 -ne $LLS2 ]; then COUNT2=`expr $COUNT2 + 1` fi fi if [[ $COUNT2 -ge $SUCCESSREPEATCOUNT || ($LLS2 -eq 0 && $COUNT2 -ge $FA ILUREREPEATCOUNT) ]]; then echo `date +%x" "%X` Uptime status will be changed for $NAME2 fr om $LLS2 CLS2=0 COUNT2=0 if [ $LLS2 -eq 1 ]; then LLS2=0 else LLS2=1 fi else CLS2=1 fi LPS2=$CPS2 ping -W $TIMEOUT -I $IP3 -c 1 $TESTIP > /dev/null 2>&1 RETVAL=$? if [ $RETVAL -ne 0 ]; then echo `date +%x" "%X` $NAME3 Down CPS3=1 else CPS3=0 fi if [ $LPS3 -ne $CPS3 ]; then echo `date +%x" "%X` Ping status changed for $NAME3 from $LPS3 t o $CPS3 COUNT3=1 else if [ $LPS3 -ne $LLS3 ]; then COUNT3=`expr $COUNT3 + 1` fi fi if [[ $COUNT3 -ge $SUCCESSREPEATCOUNT || ($LLS3 -eq 0 && $COUNT3 -ge $FA ILUREREPEATCOUNT) ]]; then echo `date +%x" "%X` Uptime status will be changed for $NAME3 fr om $LLS3 CLS3=0 COUNT3=0 if [ $LLS3 -eq 1 ]; then

LLS3=0 else LLS3=1 fi else CLS3=1 fi LPS3=$CPS3 if [[ $CLS1 -eq 0 || $CLS2 -eq 0 || $CLS3 -eq 0 ]]; then if [[ $LLS1 -eq 1 && $LLS2 -eq 0 && $LLS3 -eq 0 ]]; then echo `date +%x" "%X` Switching to $NAME2 #Se o link 1 ca ir echo "Limpando todas as regras da tabela mangle ....." iptables -F -t mangle iptables -X -t mangle echo "Regras limpas ....." ip rule del fwmark 0x10 lookup gvt prio 3 ip rule add from 192.168.3.10 table net ip rule add from 192.168.2.10 table brt ip rule add fwmark 0x30 lookup net prio 3 ip rule add fwmark 0x20 lookup brt prio 3 ip route replace default scope global nexthop via $GW2 d ev $EXTIF2 weight $W2 nexthop via $GW3 dev $EXTIF3 weight $W3 ip route flush cache #Para usar a marcao de pacotes descomente as linhas abaixo , e comente a linha acima logo aps o ltimo comando nexthop para desativar o balanc eamento do 3 link ex.: #nexthop via $GW3 dev $EXTIF3 weight $W3 #$IPTABLES -I PREROUTING -t mangle -i eth0 -s 192.168.0. 6 -j MARK --set-mark 0x30 #$IPTABLES -I PREROUTING -t mangle -i eth0 -s 192.168.0. 0/24 -d 0/0 -p tcp -m multiport --dports 110,25 -j MARK --set-mark 0x20 cat /etc/link/resolv.conf > /etc/resolv.conf elif [[ $LLS2 -eq 1 && $LLS1 -eq 0 && $LLS3 -eq 0 ]]; then echo `date +%x" "%X` Switching to $NAME1 #Se o link 2 ca ir echo "Limpando todas as regras da tabela mangle ....." iptables -F -t mangle iptables -X -t mangle echo "Regras limpas ....." ip rule del fwmark 0x20 lookup brt prio 3 ip rule add from 192.168.1.10 table gvt ip rule add from 192.168.3.10 table net ip rule add fwmark 0x10 lookup gvt prio 3 ip rule add fwmark 0x30 lookup net prio 3 ip route replace default scope global nexthop via $GW1 d ev $EXTIF1 weight $W1 nexthop via $GW3 dev $EXTIF3 weight $W3 ip route flush cache #Para usar a marcao de pacotes descomente as linhas abaixo , comente a linha acima logo aps o ltimo comando nexthop para desativar o balancea mento do 3 link ex.: #nexthop via $GW3 dev $EXTIF3 weight $W3 #$IPTABLES -I PREROUTING -t mangle -i eth0 -s 192.168.0. 6 -j MARK --set-mark 0x30 #$IPTABLES -I PREROUTING -t mangle -i eth0 -s 192.168.0. 0/24 -d 0/0 -p tcp --dport 443 -j MARK --set-mark 0x10 cat /etc/link/resolv.conf > /etc/resolv.conf

elif [[ $LLS1 -eq 0 && $LLS2 -eq 0 && $LLS3 -eq 1 ]]; then echo `date +%x" "%X` Switching to $NAME1 #Se o link 3 ca ir echo "Limpando todas as regras da tabela mangle ....." iptables -F -t mangle iptables -X -t mangle echo "Regras limpas ....." ip rule del fwmark 0x30 lookup net prio 3 ip rule add from 192.168.1.10 table gvt ip rule add from 192.168.2.10 table brt ip rule add fwmark 0x10 lookup gvt prio 3 ip rule add fwmark 0x20 lookup brt prio 3 ip route replace default scope global nexthop via $GW1 d ev $EXTIF1 weight $W1 nexthop via $GW2 dev $EXTIF2 weight $W2 ip route flush cache #Para usar a marcao de pacotes descomente as linhas abaixo . #$IPTABLES -I PREROUTING -t mangle -i eth0 -s 192.168.0. 0/24 -d 0/0 -p tcp -m multiport --dports 110,25 -j MARK --set-mark 0x20 #$IPTABLES -I PREROUTING -t mangle -i eth0 -s 192.168.0. 0/24 -d 0/0 -p tcp --dport 443 -j MARK --set-mark 0x10 cat /etc/link/resolv.conf > /etc/resolv.conf elif [[ $LLS2 -eq 0 && $LLS3 -eq 1 && $LLS1 -eq 1 ]]; then echo `date +%x" "%X` Switching to $NAME2 #Se o link 1 e 3 cair echo "Limpando todas as regras da tabela mangle ....." iptables -F -t mangle iptables -X -t mangle echo "Regras limpas ....." ip route replace default scope global via $GW2 dev $EXTIF2 ip rule del fwmark 0x10 lookup gvt prio 3 ip rule del fwmark 0x30 lookup net prio 3 ip route flush cache cat /etc/link/resolv.conf > /etc/resolv.conf elif [[ $LLS3 -eq 0 && $LLS2 -eq 1 && $LLS1 -eq 1 ]]; then echo `date +%x" "%X` Switching to $NAME3 #Se o link 1 e 2 cair echo "Limpando todas as regras da tabela mangle ....." iptables -F -t mangle iptables -X -t mangle echo "Regras limpas ....." ip route replace default scope global via $GW3 dev $EXTIF3 ip rule del fwmark 0x20 lookup brt prio 3 ip rule del fwmark 0x10 lookup gvt prio 3 ip route flush cache cat /etc/link/resolv.conf > /etc/resolv.conf elif [[ $LLS1 -eq 0 && $LLS2 -eq 1 && $LLS3 -eq 1 ]]; then echo `date +%x" "%X` Switching to $NAME1 #Se o link 2 e 3 cair echo "Limpando todas as regras da tabela mangle ....." iptables -F -t mangle iptables -X -t mangle echo "Regras limpas ....." ip route replace default scope global via $GW1 dev $EXTIF1

ip rule del fwmark 0x20 lookup brt prio 3 ip rule del fwmark 0x30 lookup net prio 3 ip route flush cache cat /etc/link/resolv.conf > /etc/resolv.conf elif [[ $LLS1 -eq 0 && $LLS2 -eq 0 && $LLS3 -eq 0 ]]; then echo `date +%x" "%X` Restoring default load balancing echo "Limpando todas as regras da tabela mangle ....." iptables -F -t mangle iptables -X -t mangle echo "Regras limpas ....." ip route replace default scope global nexthop via $GW1 d ev $EXTIF1 weight $W1 nexthop via $GW2 dev $EXTIF2 weight $W2 nexthop via $GW3 d ev $EXTIF3 weight $W3 ip rule add from 192.168.1.10 table gvt ip rule add from 192.168.2.10 table brt ip rule add from 192.168.3.10 table net ip rule add fwmark 0x10 lookup gvt prio 3 ip rule add fwmark 0x20 lookup brt prio 3 ip rule add fwmark 0x30 lookup net prio 3 ip route flush cache #Para usar a marcao de pacotes descomente as linhas abaixo , comente a linha acima logo aps o ltimo comando nexthop para desativar o balancea mento do 3 link ex.: #nexthop via $GW3 dev $EXTIF3 weight $W3 #$IPTABLES -I PREROUTING -t mangle -i eth0 -s 192.168.0. 6 -j MARK --set-mark 0x30 #$IPTABLES -I PREROUTING -t mangle -i eth0 -s 192.168.0. 0/24 -d 0/0 -p tcp -m multiport --dports 110,25 -j MARK --set-mark 0x20 #$IPTABLES -I PREROUTING -t mangle -i eth0 -s 192.168.0. 0/24 -d 0/0 -p tcp --dport 443 -j MARK --set-mark 0x10 cat /etc/link/resolv.conf > /etc/resolv.conf fi fi sleep $SLEEPTIME done

Você também pode gostar