Skip to content

Instantly share code, notes, and snippets.

@skytreader
Created October 11, 2012 15:41
Show Gist options
  • Save skytreader/3873290 to your computer and use it in GitHub Desktop.
Save skytreader/3873290 to your computer and use it in GitHub Desktop.
Quick Python script I whipped up to review for a Computer Networking exam.
BYTE = 8
def toBinaryString(num):
bitstring = ""
if num == 0:
bitstring = '0'
while num != 0:
bitstring = str(num % 2) + bitstring
num /= 2
return bitstring
def pad(bitstring, packsof):
limit = len(bitstring)
if limit >= packsof:
return bitstring
else:
return ('0' * (packsof - limit)) + bitstring
def iptobits(ipstring):
ipbits = ""
ipparts = ipstring.split(".")
i = 0
limit = len(ipparts)
while i < limit:
ipbits += pad(toBinaryString(int(ipparts[i])), BYTE)
i += 1
return ipbits
def bintodec(bitstring):
decform = 0
i = 0
limit = len(bitstring)
while i < limit:
if bitstring[i] == '1':
decform += 2 ** (limit - 1 - i)
i += 1
return decform
def bitstring_and(bstr1, bstr2):
if bstr1 == '1' and bstr2 == '1':
return '1'
else:
return '0'
def bitstring_or(bstr1, bstr2):
if bstr1 == '1' or bstr2 == '1':
return '1'
else:
return '0'
def toDottedDecimal(bitstring):
i = 0
byte_end = 8
limit = len(bitstring)
dotted_decimal = ""
while byte_end <= limit:
dotted_decimal += str(bintodec(bitstring[i:byte_end])) + '.'
i = byte_end
byte_end += 8
return dotted_decimal[0:len(dotted_decimal) - 1]
def aggregate(broken_ip):
i = 1
limit = len(broken_ip)
aggregate = broken_ip[0]
while i < limit:
aggregate += '.' + broken_ip[i]
i += 1
return aggregate
def read_ipmask(ipmaskstring):
ipmask_dichotomy = ipmaskstring.split('/')
ip = ipmask_dichotomy[0]
ipbits = iptobits(ip)
mask = int(ipmask_dichotomy[1])
netmask = ('1' * mask) + ('0' * (32 - mask))
net_address = ""
i = 0
limit = len(ipbits)
while i < limit:
net_address += bitstring_and(ipbits[i], netmask[i])
i += 1
net_address = toDottedDecimal(net_address)
#flip the netmask now
i = 0
flipped_mask = ""
while i < limit:
if netmask[i] == '1':
flipped_mask += '0'
else:
flipped_mask += '1'
i += 1
#get the broadcast address, bitstring_or the flipped
#netmask with ipbits.
i = 0
broadcast = ""
while i < limit:
broadcast += bitstring_or(ipbits[i], flipped_mask[i])
i += 1
broadcast = toDottedDecimal(broadcast)
print "IP:", ipmaskstring
print "Netmask:", netmask, "=", toDottedDecimal(netmask)
print "Net Address:", net_address
print "Broadcast Address:", broadcast
#Compute the Host address
na_split = net_address.split('.')
first_address = int(na_split[len(na_split) - 1]) + 1 #Handle overflow!
na_split[len(na_split) - 1] = str(first_address)
bc_split = broadcast.split('.')
last_address = int(bc_split[len(bc_split) - 1]) - 1
bc_split[len(bc_split) - 1] = str(last_address)
print "Host Address:", aggregate(na_split), "to", aggregate(bc_split)
InPut = raw_input("Enter IP/Mask: ")
while InPut != "#":
read_ipmask(InPut)
InPut = raw_input("Enter IP/Mask: ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment