219 |
jeremy |
1 |
#!/bin/bash
|
|
|
2 |
# $Id: alcasar-load_balancing.sh 221 2010-08-31 06:55:40Z franck $
|
221 |
franck |
3 |
|
219 |
jeremy |
4 |
# alcasar-load_balancing.sh Load-balance internet connection over two or more local links
|
|
|
5 |
#
|
|
|
6 |
# Version: 0.8.0 - Wed, Sep 01, 2010
|
|
|
7 |
#
|
|
|
8 |
# by Author: BOUIJOUX Franck (3abTux) <3abtux@free.fr>
|
|
|
9 |
# en cours d'expérimentation et d 'écriture
|
|
|
10 |
#
|
|
|
11 |
|
|
|
12 |
# Définion des poids des routes
|
|
|
13 |
WEIGHT1=1
|
|
|
14 |
WEIGHT2=1
|
221 |
franck |
15 |
#WEIGHT3=3
|
219 |
jeremy |
16 |
|
|
|
17 |
# Set devices:
|
|
|
18 |
DEV1=${1-eth0} # defaut eth0
|
|
|
19 |
DEV2=${2-eth0} # defaut eth0 mais peut être autre chose :-)
|
|
|
20 |
DEV3=${3-eth0} # defaut eth0 mais peut être autre chose :-)
|
|
|
21 |
|
|
|
22 |
# Trouver les adresses pour chaque interface
|
|
|
23 |
IP1=`ifconfig $DEV1 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
|
|
|
24 |
IP2=`ifconfig $DEV2 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
|
|
|
25 |
#IP3=`ifconfig $DEV3 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
|
|
|
26 |
|
|
|
27 |
# iTrouver les passerelles pour chaque interface
|
|
|
28 |
#GW1=`route -n | grep $DEV1 | grep '^0.0.0.0' | awk '{ print $2 }'`
|
|
|
29 |
#GW2=`route -n | grep $DEV2 | grep '^0.0.0.0' | awk '{ print $2 }'`
|
|
|
30 |
GW1=192.168.1.6
|
|
|
31 |
GW2=192.168.1.1
|
|
|
32 |
#GW3=192.168.1.6
|
|
|
33 |
|
|
|
34 |
echo "Acces internet depuis $DEV1: IP=$IP1 par la GW=$GW1"
|
|
|
35 |
echo " et depuis $DEV2: IP=$IP2 par la GW=$GW2"
|
|
|
36 |
#echo " et depuis $DEV3: IP=$IP3 par la GW=$GW3"
|
|
|
37 |
|
|
|
38 |
# Definition des routes
|
|
|
39 |
|
|
|
40 |
# Tester si les tables existent sinon les créer
|
|
|
41 |
if [ -z "`cat /etc/iproute2/rt_tables | grep '^252'`" ] ; then
|
|
|
42 |
echo "252 rt_dev1" >> /etc/iproute2/rt_tables
|
|
|
43 |
fi
|
|
|
44 |
|
|
|
45 |
if [ -z "`cat /etc/iproute2/rt_tables | grep '^251'`" ] ; then
|
|
|
46 |
echo "251 rt_dev2" >> /etc/iproute2/rt_tables
|
|
|
47 |
fi
|
|
|
48 |
#if [ -z "`cat /etc/iproute2/rt_tables | grep '^250'`" ] ; then
|
|
|
49 |
# echo "250 rt_dev3" >> /etc/iproute2/rt_tables
|
|
|
50 |
#fi
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
# Tables de routage
|
|
|
54 |
ip route add default via $GW1 table rt_dev1
|
|
|
55 |
ip route add default via $GW2 table rt_dev2
|
|
|
56 |
#ip route add default via $GW3 table rt_dev3
|
|
|
57 |
|
|
|
58 |
# Créatin des règles
|
|
|
59 |
ip rule add from $IP1 table rt_dev1
|
|
|
60 |
ip rule add from $IP2 table rt_dev2
|
|
|
61 |
#ip rule add from $IP3 table rt_dev3
|
|
|
62 |
|
|
|
63 |
# Effacer la route par défaut existante
|
|
|
64 |
if [ ! -z "`ip route show table main | grep 'nexthop'`" ] ; then
|
|
|
65 |
ip route del default scope global
|
|
|
66 |
fi
|
|
|
67 |
|
|
|
68 |
# Alterne les liens basés sur chauqe route
|
|
|
69 |
ip route add default scope global nexthop via $GW1 dev $DEV1 weight $WEIGHT1 \
|
|
|
70 |
nexthop via $GW2 dev $DEV2 weight $WEIGHT2
|
|
|
71 |
# nexthop via $GW3 dev $DEV3 weight $WEIGHT3
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
# Purge le cache
|
|
|
75 |
ip route flush cache
|
|
|
76 |
|
|
|
77 |
# Fin de alcasar-load_balancing.sh
|