Script to calculate default gateway according to IP

Taking care of your Linux box.

Script to calculate default gateway according to IP

Postby osama » Wed Feb 25, 2009 10:47 am

Hello,

I have a dynamic ip address for one of 2 External NICs. I have to change gateway accordingly when one goes down. I have to calculate gateway according to assigned IP and BROADCAST address. Can any one give me a script to calculate gateway according to assigned IP and Broadcast address?

Regards,
osama
Havaldaar
 
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am

Postby mejam » Wed Feb 25, 2009 11:32 am

i have posted a Load-Balancing and Fail-over script in the forum...search for that it will help u.
Regards
Abdulrehman
mejam
Havaldaar
 
Posts: 127
Joined: Sat Oct 18, 2008 12:30 pm
WLM: bmw1000cc@hotmail.com
Yahoo Messenger: vagabond_aw@yahoo.com
Location: Lahore

Postby osama » Wed Feb 25, 2009 12:06 pm

cant find. Give me the link
osama
Havaldaar
 
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am

Postby mejam » Wed Feb 25, 2009 12:27 pm

Regards

Abdulrehman
mejam
Havaldaar
 
Posts: 127
Joined: Sat Oct 18, 2008 12:30 pm
WLM: bmw1000cc@hotmail.com
Yahoo Messenger: vagabond_aw@yahoo.com
Location: Lahore

Postby osama » Wed Feb 25, 2009 12:42 pm

No help from that script. I need gateway address
osama
Havaldaar
 
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am

Postby lambda » Wed Feb 25, 2009 1:25 pm

did you mean to say broadcast address, or netmask?

for me,

Code: Select all

13:23:11% ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:16:17:D7:85:F2 
          inet addr:192.168.1.10  Bcast:192.168.1.255  Mask:255.255.255.0
...

my gateway address could be anywhere from 192.168.1.1 to 192.168.1.255 with the exception of 192.168.1.10.
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby osama » Wed Feb 25, 2009 2:49 pm

Like if my IP is 192.168.10.111 then gateway will be 192.168.10.1
Like if my IP is 10.10.10.111 then gateway will be 10.10.10.1


I want to get first IP i.e *.1 of any network.
osama
Havaldaar
 
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am

Postby lambda » Wed Feb 25, 2009 10:19 pm

then the broadcast address doesn't matter.

gw.sh:

Code: Select all

#!/bin/sh

ip=$1
IFS=.
set $ip
echo $1.$2.$3.1
run it as gw.sh 192.168.1.10
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby osama » Thu Feb 26, 2009 10:37 am

Thanx :)
osama
Havaldaar
 
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am

Postby osama » Fri Mar 06, 2009 10:16 am

Hey lambda consider following scenario

inet addr:192.168.55.75 Bcast:192.168.55.127 Mask:255.255.255.192

Gateway for this network is 192.168.55..65

That is the first IP of the network.

Can u give me a script for this
osama
Havaldaar
 
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am

Postby lambda » Fri Mar 06, 2009 2:50 pm

Code: Select all

#!/usr/bin/env python

import os.path
import sys

# gateway is one ip above the lowest ip
GWIPADD = 1


def usage(msg=None):
    prog = os.path.basename(sys.argv[0])
    if msg:
        sys.stderr.write('%s\n' % msg)
    sys.stderr.write('''\
usage: %s ip mask
e.g.: %s 192.168.55.75 255.255.255.192
''' % (prog, prog))
    sys.exit(1)


def dotsplit(thing, whatisit):
    tmpls = thing.split('.')
    if len(tmpls) != 4:
        usage('invalid %s: %s' % (whatisit, thing))
    tmpls = [int(octet) for octet in tmpls]
    if max(tmpls) > 255 or min(tmpls) < 0:
        usage('invalid %s: %s' % (whatisit, thing))
    return tmpls
   

def main(args):
    if len(args) != 2:
        usage()

    addr = dotsplit(args[0], 'ip')
    mask = dotsplit(args[1], 'mask')

    gwaddr = []
    for i in range(4):
        gwaddr.append(addr[i] & mask[i])
    gwaddr[3] += GWIPADD

    print '.'.join([str(octet) for octet in gwaddr])
    sys.exit(0)

if __name__ == '__main__':
    main(sys.argv[1:])
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby osama » Fri Mar 06, 2009 3:32 pm

:)
osama
Havaldaar
 
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am


Return to “%s” Administration

Who is online

Users browsing this forum: No registered users and 2 guests

cron