Script to calculate default gateway according to IP

Taking care of your Linux box.
Post Reply
osama
Havaldaar
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am

Script to calculate default gateway according to IP

Post by osama »

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,
mejam
Havaldaar
Posts: 127
Joined: Sat Oct 18, 2008 12:30 pm
Location: Lahore
Contact:

Post by mejam »

i have posted a Load-Balancing and Fail-over script in the forum...search for that it will help u.
Regards
Abdulrehman
osama
Havaldaar
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am

Post by osama »

cant find. Give me the link
mejam
Havaldaar
Posts: 127
Joined: Sat Oct 18, 2008 12:30 pm
Location: Lahore
Contact:

Post by mejam »

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

Post by osama »

No help from that script. I need gateway address
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

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"?
osama
Havaldaar
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am

Post by osama »

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.
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

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"?
osama
Havaldaar
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am

Post by osama »

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

Post by osama »

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
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

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"?
osama
Havaldaar
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am

Post by osama »

:)
Post Reply