LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2005, 04:10 PM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
Lightbulb load balance with DHCP connections


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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i load balance 3 WAN connections? touser Linux - Networking 4 09-05-2005 05:39 AM
network load balance with DHCP connections eantoranz Programming 2 06-04-2005 01:20 PM
Load balance 2 dial-up connections morosband Linux - General 2 03-04-2005 05:09 PM
Load balance 2 dial-up connections morosband Linux - Networking 1 03-03-2005 11:58 PM
Load Balance with BSD rpaula *BSD 1 03-22-2004 09:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 10:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration