salawank

ip_calc.py

Jan 7th, 2014
129
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import ipcalc
  4. from ipcalc import *
  5. from netaddr import *
  6. import sys
  7.  
  8. def how_to():
  9.     print '\nUsage: ipcalc [-s|-v] <ADDRESS>/<NETMASK>\n -s \t Summary of ipcalc output \n -v \t Verbose ipcalc output with IP addresses'
  10.     print '\nExamples:\nipcalc -s 192.168.0.1/24\nipcalc -v 192.168.0.1/29\nipcalc -s 192.168.0.1/255.255.255.248\nipcalc -v 192.168.0.1/255.255.255.240'
  11.  
  12. if len(sys.argv) <= 1:
  13.     how_to()
  14.     sys.exit(1)
  15.  
  16. def main():
  17.     try:
  18.         input = sys.argv[2]
  19.         opt = sys.argv[1]
  20.         print '\nIPCalc - tdr(dot)local(at)gmail.com'
  21.         print '-----------------------------------'
  22.         global ips
  23.         global ip
  24.         ips = Network(input)
  25.         ip = IPNetwork(input)
  26.         #oper()
  27.         if opt == '-s':
  28.             oper()
  29.         elif opt == '-v':
  30.             print '\nVerbose output:'
  31.             oper()
  32.             for x in ipcalc.Network(input):
  33.                 print str(x)
  34.         else:
  35.             how_to()
  36.     except ValueError:
  37.         print 'Invalid input!'
  38.         how_to()
  39.     except IndexError:
  40.         how_to()
  41.     except:
  42.         print 'Invalid input!'
  43.  
  44. def oper():
  45.     print '\nAddress: '+str(ips)
  46.     print 'Netmask: '+str(ips.netmask())
  47.     print 'Network: '+str(ip.cidr)
  48.     print 'Broadcast: '+str(ips.broadcast())
  49.     print 'HostMin: '+str(ips.host_first())
  50.     print 'HostMax: '+str(ips.host_last())
  51.     print 'Hosts/Net: '+str(ips.size())+'\n'
  52.     #print 'IP Version '+str(ip.version)
  53.  
  54. if __name__ == '__main__':
  55.   main()
RAW Paste Data