1 |
#!/bin/bash
|
1 |
#!/bin/bash
|
2 |
# $Id: alcasar-load_balancing.sh 937 2012-07-05 22:39:11Z franck $
|
2 |
# $Id: alcasar-load_balancing.sh 938 2012-07-05 22:40:33Z franck $
|
3 |
|
3 |
|
4 |
# alcasar-load_balancing.sh
|
4 |
# alcasar-load_balancing.sh
|
5 |
# by BOUIJOUX Franck (3abTux) <3abtux@free.fr>
|
5 |
# by BOUIJOUX Franck (3abTux) <3abtux@free.fr>
|
6 |
# This script is distributed under the Gnu General Public License (GPL)
|
6 |
# This script is distributed under the Gnu General Public License (GPL)
|
7 |
|
7 |
|
8 |
# Équilibrage de charge sur 2 ou plusieurs liens internet (en cours d'expérimentation)
|
8 |
# Équilibrage de charge sur 2 ou plusieurs liens internet (en cours d'expérimentation)
|
9 |
# Définion des poids des routes : même poids --> alternance des connexions. Sinon le poids le plus faible est prioritaire
|
9 |
# Définion des poids des routes : même poids --> alternance des connexions. Sinon le poids le plus faible est prioritaire
|
10 |
# Load balancing with 2 or more Internet links (experimental)
|
10 |
# Load balancing with 2 or more Internet links (experimental)
|
11 |
# Weitght of routes : same --> switch between link. The lowest weight gain the priority.
|
11 |
# Weitght of routes : same --> switch between link. The lowest weight gain the priority.
|
12 |
|
12 |
|
13 |
WEIGHT1=1
|
13 |
WEIGHT1=1
|
14 |
WEIGHT2=1
|
14 |
WEIGHT2=1
|
15 |
#WEIGHT3=3
|
15 |
#WEIGHT3=3
|
16 |
|
16 |
|
17 |
# Définition des interfaces :
|
17 |
# Définition des interfaces :
|
18 |
DEV1=${1-eth0} # defaut eth0
|
18 |
DEV1=${1-eth0} # defaut eth0
|
19 |
DEV2=${2-eth0} # defaut eth0 mais peut être autre chose :-)
|
19 |
DEV2=${2-eth0:1} # defaut eth0 mais peut être autre chose :-)
|
20 |
#DEV3=${3-eth0} # defaut eth0 mais peut être autre chose :-)
|
20 |
#DEV3=${3-eth0:2} # defaut eth0 mais peut être autre chose :-)
|
21 |
|
21 |
|
22 |
# Trouver les adresses pour chaque interface
|
22 |
# Trouver les adresses pour chaque interface
|
23 |
IP1=`ifconfig $DEV1 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
|
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 }'`
|
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 }'`
|
25 |
#IP3=`ifconfig $DEV3 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
|
26 |
|
26 |
|
27 |
# Trouver les passerelles pour chaque interface { ne fonctionne pas bien avec une seule interface } --> forcer les passerelles !
|
27 |
# Trouver les passerelles pour chaque interface { ne fonctionne pas bien avec une seule interface } --> forcer les passerelles !
|
28 |
#GW1=`route -n | grep $DEV1 | grep '^0.0.0.0' | awk '{ print $2 }'`
|
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 }'`
|
29 |
#GW2=`route -n | grep $DEV2 | grep '^0.0.0.0' | awk '{ print $2 }'`
|
30 |
GW1=192.168.1.1
|
30 |
GW1=192.168.1.1
|
31 |
GW2=192.168.1.6
|
31 |
GW2=192.168.1.6
|
32 |
#GW3=192.168.1.6
|
32 |
#GW3=192.168.1.6
|
33 |
|
33 |
|
34 |
echo "Acces internet depuis $DEV1: IP=$IP1 par la GW=$GW1"
|
34 |
echo "Acces internet depuis $DEV1: IP=$IP1 par la GW=$GW1"
|
35 |
echo " et depuis $DEV2: IP=$IP2 par la GW=$GW2"
|
35 |
echo " et depuis $DEV2: IP=$IP2 par la GW=$GW2"
|
36 |
#echo " et depuis $DEV3: IP=$IP3 par la GW=$GW3"
|
36 |
#echo " et depuis $DEV3: IP=$IP3 par la GW=$GW3"
|
37 |
|
37 |
|
38 |
# Mise en place des routes
|
38 |
# Mise en place des routes
|
39 |
|
39 |
|
40 |
# Tester si les tables existent sinon les créer
|
40 |
# Tester si les tables existent sinon les créer
|
41 |
if [ -z "`cat /etc/iproute2/rt_tables | grep '^252'`" ] ; then
|
41 |
if [ -z "`cat /etc/iproute2/rt_tables | grep '^252'`" ] ; then
|
42 |
echo "252 rt_dev1" >> /etc/iproute2/rt_tables
|
42 |
echo "252 rt_dev1" >> /etc/iproute2/rt_tables
|
43 |
fi
|
43 |
fi
|
44 |
|
44 |
|
45 |
if [ -z "`cat /etc/iproute2/rt_tables | grep '^251'`" ] ; then
|
45 |
if [ -z "`cat /etc/iproute2/rt_tables | grep '^251'`" ] ; then
|
46 |
echo "251 rt_dev2" >> /etc/iproute2/rt_tables
|
46 |
echo "251 rt_dev2" >> /etc/iproute2/rt_tables
|
47 |
fi
|
47 |
fi
|
48 |
#if [ -z "`cat /etc/iproute2/rt_tables | grep '^250'`" ] ; then
|
48 |
#if [ -z "`cat /etc/iproute2/rt_tables | grep '^250'`" ] ; then
|
49 |
# echo "250 rt_dev3" >> /etc/iproute2/rt_tables
|
49 |
# echo "250 rt_dev3" >> /etc/iproute2/rt_tables
|
50 |
#fi
|
50 |
#fi
|
51 |
|
51 |
|
52 |
|
52 |
|
53 |
# Tables de routage
|
53 |
# Tables de routage
|
54 |
ip route add default via $GW1 table rt_dev1
|
54 |
ip route add default via $GW1 table rt_dev1
|
55 |
ip route add default via $GW2 table rt_dev2
|
55 |
ip route add default via $GW2 table rt_dev2
|
56 |
#ip route add default via $GW3 table rt_dev3
|
56 |
#ip route add default via $GW3 table rt_dev3
|
57 |
|
57 |
|
58 |
# Création des règles
|
58 |
# Création des règles
|
59 |
ip rule add from $IP1 table rt_dev1
|
59 |
ip rule add from $IP1 table rt_dev1
|
60 |
ip rule add from $IP2 table rt_dev2
|
60 |
ip rule add from $IP2 table rt_dev2
|
61 |
#ip rule add from $IP3 table rt_dev3
|
61 |
#ip rule add from $IP3 table rt_dev3
|
62 |
|
62 |
|
63 |
# Effacer la route par défaut existante
|
63 |
# Effacer la route par défaut existante
|
- |
|
64 |
ip route del default
|
64 |
if [ ! -z "`ip route show table main | grep 'nexthop'`" ] ; then
|
65 |
if [ ! -z "`ip route show table main | grep 'nexthop'`" ] ; then
|
65 |
ip route del default scope global
|
66 |
ip route del default scope global
|
66 |
fi
|
67 |
fi
|
67 |
|
68 |
|
68 |
# Alterne les liens basés sur chaque route
|
69 |
# Alterne les liens basés sur chaque route
|
69 |
ip route add default scope global nexthop via $GW1 dev $DEV1 weight $WEIGHT1 \
|
70 |
ip route add default scope global nexthop via $GW1 dev $DEV1 weight $WEIGHT1 \
|
70 |
nexthop via $GW2 dev $DEV2 weight $WEIGHT2
|
71 |
nexthop via $GW2 dev $DEV2 weight $WEIGHT2
|
71 |
# nexthop via $GW3 dev $DEV3 weight $WEIGHT3
|
72 |
# nexthop via $GW3 dev $DEV3 weight $WEIGHT3
|
72 |
|
73 |
|
73 |
|
74 |
|
74 |
# Purge le cache
|
75 |
# Purge le cache
|
75 |
ip route flush cache
|
76 |
ip route flush cache
|
76 |
|
77 |
|
77 |
# Fin de alcasar-load_balancing.sh
|
78 |
# Fin de alcasar-load_balancing.sh
|
78 |
|
79 |
|
79 |
Generated by GNU Enscript 1.6.6.
|
80 |
Generated by GNU Enscript 1.6.6.
|
80 |
|
81 |
|
81 |
|
82 |
|
82 |
|
83 |
|