Script to calculate default gateway according to IP
Script to calculate default gateway according to IP
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,
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,
did you mean to say broadcast address, or netmask?
for me,
my gateway address could be anywhere from 192.168.1.1 to 192.168.1.255 with the exception of 192.168.1.10.
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
...
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"?
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
then the broadcast address doesn't matter.
gw.sh:run it as gw.sh 192.168.1.10
gw.sh:
Code: Select all
#!/bin/sh
ip=$1
IFS=.
set $ip
echo $1.$2.$3.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"?
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
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"?
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?