LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


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

Rep: Reputation: 90
network load balance with DHCP connections


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!

Last edited by eantoranz; 06-03-2005 at 02:18 PM.
 
Old 06-04-2005, 05:00 AM   #2
Thoreau
Senior Member
 
Registered: May 2003
Location: /var/log/cabin
Distribution: All
Posts: 1,167

Rep: Reputation: 45
This reminds me of an old saying. Fix the problem, not the blame.

What are you trying to do exactly? You say that you have 3 internet connection that you want to load balance, but don't describe them. Please tell us what you want to do first, and then what is available to you aka what hardware you are using.

What you are describing is a ghetto/unprofessional kludge.
 
Old 06-04-2005, 01:20 PM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092

Original Poster
Blog Entries: 1

Rep: Reputation: 90
Well.... at the office we have three ADSL modems, supplied by our ISP. They must be routers, as a matter of fact... but don't care about that.

They provide DHCP through ethernet connections. Two of them provide 1500+ bps and another provides 768 bps.

I want to set a linux box that can handel them.

The problem is that the routes are not static, therefore, I have written an script (look above) that will react to DHCP events (as a matter of fact, it's a hook to dhclient-script) and refresh the default multipath route of the linux box.

Any other thing you would like to know?
 
  


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
dns load and balance kafnir Linux - Networking 1 10-13-2005 12:41 PM
How do i load balance 3 WAN connections? touser Linux - Networking 4 09-05-2005 05:39 AM
load balance with DHCP connections eantoranz Linux - Networking 0 06-02-2005 04:10 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:51 PM.

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