Script para balanceamento de links sobre duas conexões internet

#!/bin/bash
#
#Autor: Rodrigo Kiyoshi
#
#Data.: 01/03/2010
#

# Defina as interfaces:
INET1_DV=${1-eth0}    # padrão eth0
INET2_DV=${2-ppp0}    # padrão ppp0

# Capture os IPs das interfaces:
INET1_IP=`ifconfig $INET1_DV | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
INET2_IP=`ifconfig $INET2_DV | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`

# Capture o gateway default das interfaces:
INET1_GW=`route -n | grep $INET1_DV | grep '^0.0.0.0' | awk '{ print $2 }'`
INET2_GW=`route -n | grep $INET2_DV | grep '^0.0.0.0' | awk '{ print $2 }'`

### Definição das rotas ###

# Verifique se as tabelas existem, se não -> crie as duas tabelas:
if [ -z "`cat /etc/iproute2/rt_tables | grep '^251'`" ] ; then
   echo "251    rt_inet1_dv" >> /etc/iproute2/rt_tables
fi
if [ -z "`cat /etc/iproute2/rt_tables | grep '^252'`" ] ; then
   echo "252    rt_inet2_dv" >> /etc/iproute2/rt_tables
fi

# Defina a rota das tabelas:
ip route add default via $INET1_GW table rt_inet1_dv
ip route add default via $INET2_GW table rt_inet2_dv

# Crie as regras:
ip rule add from $INET1_IP table rt_inet1_dv
ip rule add from $INET2_IP table rt_inet2_dv

# Se já temos uma rota 'nexthop', apague a mesma:
if [ ! -z "`ip route show table main | grep 'nexthop'`" ] ; then
   ip route del default scope global
fi

# Balance os links baseado nas rotas:
ip route add default scope global nexthop via $INET1_GW dev $INET1_DV weight 1 nexthop via $INET2_GW dev $INET2_DV weight 1

# Limpe o cache da tabela:
ip route flush cache