This is a networking problem.... but It involves making a script. I posted a "RFC" in the networking forum so people could comment on it.... but no one has.... so I decided to come here to see if you can give me a hand.
Here at the office, I will set a computer that will handle three different internet connections that will have to be used to share their bandwith. The problem is that the network configurations are stablished by DHCP my our ISP, therefore, I can't set a static default route.
So I made a script that will be called by dhclient everytime one interface's configuration changes. I want to post it here so you can make comments so I can improve it.
I'm not a bash script guru. This is my first "serious business" script, so I bet there are things that can be done in an easire way.... but this is what I was able to puit together today.
The project is called "dhep" (I work in a hospital called "Hospital de Especialidades Pediátricas.... or Hospital of Pediatric Specialties.. I guess you can figure out where the name came from
).
the script:
Code:
#this 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
This file will use a file called /etc/dhep/interfaces where the interfaces that have to deal with this mess will be listed with options for the nexthop in case it be necessary (like weight).
It will use for every interface a file called $interface.cfg where the script will save the update time, the IP and the gateway IP.
Well... I'm looking forward to hearing from you.
Thanks in advance!