BrandonSpendlove

Simple IP Subnet calculator - 1st Subnet

Oct 7th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. def getSubnetInfo(ipaddr, subnet_mask):
  2.     splitMask = map(int, subnet_mask.split('.'))
  3.     splitIpaddr = map(int, ipaddr.split('.'))
  4.  
  5.     #Find index of magic octet in splitMask(that isn't = to 255)
  6.     magicOctet = 0
  7.     for octet in splitMask:
  8.         if octet == 255:
  9.             magicOctet = magicOctet + 1
  10.             #Value in this index = 255, so search next index          
  11.  
  12.     increment = 256 - splitMask[magicOctet]
  13.     ipaddrOctet = int(ipaddr.split('.')[magicOctet])
  14.  
  15.     #Keeping track of variables
  16.     print "Original IP: " + str(ipaddr)
  17.     #print "Magic Octet Index: " + str(magicOctet)
  18.     #print "Magic number: " + str(ipaddrOctet)
  19.     #print "Increments: " + str(increment)
  20.     #==============================================#
  21.  
  22.     networkID = 0
  23.     while ipaddrOctet > networkID:
  24.         networkID = networkID + increment
  25.     networkID = networkID - increment
  26.     broadcast = (networkID + increment) - 1
  27.  
  28.     print("Network = {}.{}.{}.{}".format(splitIpaddr[0], splitIpaddr[1], splitIpaddr[2], networkID))
  29.     print("First Valid IP = {}.{}.{}.{}".format(splitIpaddr[0], splitIpaddr[1], splitIpaddr[2], networkID + 1))
  30.     print("Broadcast = {}.{}.{}.{}".format(splitIpaddr[0], splitIpaddr[1], splitIpaddr[2], broadcast))
  31.  
  32. getSubnetInfo("192.168.123.10", "255.255.255.224")
Advertisement
Add Comment
Please, Sign In to add comment