Hi, Guys!
I wanted to write to you so you give me some feedback on something I'm working on.
I have been given the task to implement a linux box to handle three different internet connections so that we can share the load between them.
After some wild experimenting and reading about multipath (though I think not a lot of detailed information is available), I have been able to achieve the load balance.
The only problem I'm facing right now is that the ISPs connections are configured with DHCP.
That created a whole new level of dificulty, but I think I have programmed one viable (though not perfect) dhclient hook to take care of the default GW once one of the DHCP configuration changes.
I haven't tested it yet... but I think it will do. I will test it tomorrow. But I wanted to hear your feedback.
The "project" is called dhep. I work in a Hospital called "Hospital de Especialidades Pediatricas" (Hospital of Pediatric Specialties)... I think you can figure out where the project name came from, right?
.
Anyway.... this is the main script. The one that will be hooked to dhclient-exit-hooks.
Code:
#his script will be called by dhclient whenever one of the DHCP configurations changes
#for further information, see `man dhclient-script`.
# received values
# $interface
# Name of the interface
# $reason
# Reason of the invocation of the call
# MEDIUM
# PREINIT
# BIND
# RENEW
# REBIND
# REBOOT
# EXPIRE
# FAIL
# TIMEOUT
# $new_ip_address
# new IP ADDRESS
# $new_routers
# new routers
#Check wether this interface matters to us.
# it must be listed in the file /etc/dhep/interfaces
echo DHEP Interface: $interface
echo DHEP Reason: $reason
# If the interface is not listed, get out!
pattern=^$interface
showsup=$(grep $pattern /etc/dhep/interfaces | wc -l)
if [ $showsup = 0 ]; then
echo This interface is not important to us
echo Goint out
exit
else
echo This interface DOES matter
fi
#file with this interface's configuration
config=/etc/dhep/$interface.cfg
case $reason in
PREINIT | EXPIRE | FAIL)
# We clean up the interface's configuration file
echo Cleaning up interface\'s configuration file
echo date $(date) > $config
;;
REBOOT | BIND | RENEW | REBIND)
# we have grabbed an address from the DHCP server
echo date $(date) > $config
echo IP $new_ip_address >> $config
echo GW $new_routers >> $config
;;
esac
#let's process the routes
grep '^[ \t]*[a-zA-Z]' /etc/dhep/interfaces | while read an_interface_line; do
#for an_interface in $(grep '^[ \t]*[a-zA-Z]' /etc/dhep/interfaces); do
# let's look for the interface's line in the interfaces file
length=$(expr length "$an_interface_line")
interfacestart=$(expr index "$an_interface_line" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ])
interfaceend=$(expr index "$an_interface_line" ' ')
if [ $interfaceend = 0 ]; then
# no options
interfacelength=$(($length-$interfacestart+1))
interface=$(expr substr $an_interface_line $interfacestart $interfacelength)
options=""
else
# interface and options
interfacelength=$(($interfaceend-$interfacestart))
interface=$(expr substr "$an_interface_line" $interfacestart $interfacelength)
optionslength=$(($length-$interfaceend+1))
options=$(expr substr "$an_interface_line" $(($interfaceend+1)) $optionslength
fi
echo Interface: $interface
echo Options: $options
# Let's get this interface's configuration
config=/etc/dhep/$interface.cfg
if [ $(grep IP $config | wc -l ) != 0 ]; then
echo This card is configured as nexthop
# IP?
oneline=$(cat $config | grep IP)
ip=$(expr substr "$oneline" 4 15)
echo IP $ip
oneline=$(cat $config | grep GW)
gw=$(expr substr "$oneline" 4 15)
echo GW $gw
nexthop=$nexthop\ nexthop\ via\ $gw\ dev\ $interface\ $options
fi
done
#removing default GWs
ip route del default
# let's set the route
ip route add scope global default $nexthop
# let's flush teh route cache
ip route flush cache
Some important information
There will be a file called /etc/dhep/interfaces listing the interfaces that will be taken in consideration by this script... plus nexthop options.... most likely weight.
Code:
eth1 weight 2
eth2 weight 2
eth3 weight 1
eth5
interfaces not listed there, will cause the script to finish without modifying the routing table.
For every interface, there will be a configuration file created by this script where the script will save the address of the gateway through that interface, the IP adress (just in case), and the time of last update.
Well.... I'm willing to hear from you.
Talk later!
PS I'm not a bash script master. This is the result of asking a lot of questions, reading a lot of man pages and google digging today. This is my first "important" bash script ever. I'm sure there are more efective ways to do things. Would appreciate if you tell me about improvements too.
Thanks in advance!