Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from socket import inet_aton, inet_ntoa
- from struct import unpack, pack
- # might need to make an IP tools module for now it's all here
- def ip2long(ip_addr):
- return unpack("!L", inet_aton(ip_addr))[0]
- def long2ip(ip):
- return inet_ntoa(pack("!L", ip))
- ####################################################
- # my helper functions
- def subNetInfo(network, mask):
- # takes the network address, and return information about the subnet
- # Bcast
- # number of possible subnets
- # number of possible hosts
- # want to be able to send the values as either a string or a long
- try:
- intNetwork = ip2long(network)
- except TypeError:
- intNetwork = network
- try:
- # take the string mask, and convert it into a bit mask
- temp = ip2long(mask)
- intMask = 0
- while(temp != 0):
- print bin(temp)[2:].zfill(32)
- temp = long(temp << 1)
- intMask += 1
- except TypeError:
- intMask = mask
- # at this point the network address should be a long and the mask is int
- # here I do two things, shift the bits, and get the reverse mask 1100 would become 0011 the I use the OR to get the bCast
- bCast = intNetwork ^ long(4294967295 >> intMask ) # 4294967295 == 2**32-1 I use this as a constant
- addresses = 2**(32-intMask)
- subnets = 2**(intMask-8)
- # lets package this in a dict for easy display
- result = {'bcast': bcast, 'hosts': addresses-2, 'subnets': subnets}
- return result
- print subNetInfo('10.0.0.0', '255.255.255.0')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement