Skip to content

Instantly share code, notes, and snippets.

@TannerRayMartin
Last active September 30, 2016 09:34
Show Gist options
  • Save TannerRayMartin/dbea55da3828201d45b2447001fb4348 to your computer and use it in GitHub Desktop.
Save TannerRayMartin/dbea55da3828201d45b2447001fb4348 to your computer and use it in GitHub Desktop.
prints the network, first host, last host, and broadcast of a valid IP address
#this program is currently without comments- this is the first draft and will be simplified and added to.
#constructed by Tanner Martin- 9-30-16 - 4 hour completion time.
import random
import time
import os
print('example: 192.168.3.5/26')
print('\n')
yellow = '\033[1;33;49m'
green = '\033[1;32;49m'
light_blue = '\033[1;36;49m'
purple = '\033[1;35;49m'
white = '\033[1;37;49m'
ip_address = raw_input('ipaddress/cider: ')
class subnet:
def __init__(self):
pass
def convert(self, ip):
l1 = []
l2 = []
s1 = ''
s2 = ''
s3 = ''
s4 = ''
cider = ''
num = 1
for i in ip:
if num == 1:
if i == '.':
num += 1
else:
s1 = s1 + i
elif num == 2:
if i == '.':
num += 1
else:
s2 = s2 + i
elif num == 3:
if i == '.':
num += 1
else:
s3 = s3 + i
elif num == 4:
if i == '/':
num += 1
else:
s4 = s4 + i
elif num == 5:
cider = cider + i
s1 = int(s1)
s2 = int(s2)
s3 = int(s3)
s4 = int(s4)
cider = int(cider)
l1.append(s1)
l1.append(s2)
l1.append(s3)
l1.append(s4)
l1.append(cider)
return(l1)
def binary(self, num):
binary_list = [128,64,32,16,8,4,2,1]
s1 = ''
for i in binary_list:
if num < i:
s1 = s1 + '0'
elif num >= i:
s1 = s1 + '1'
num = num - i
return s1
def get_block_size(self, cider):
ciderList = [128,64,32,16,8,4,2,1,128,64,32,16,8,4,2,1,128,64,32,16,8,4,2,1,128,64,32,16,8,4,2,1]
cider -= 1
block_size = ciderList[cider]
return block_size
def get_network_span(self, information, block_size):
cider0 = str(information[4])
classA = False
classB = False
classC = False
spanning = True
if information[4] > 8 and information[4] <17:
new_cider = 8 + information[4]
classA = True
elif information[4] > 16 and information[4] < 25:
new_cider = 16 + information[4]
classB = True
elif information[4] > 24 and information[4] < 33:
new_cider = 24 + information[4]
classC = True
num = 0
num2 = 0
num3 = 0
if classA == True:
while num < 255:
if information[1] >= num:
span_max = num + block_size -1
if information[1] <= span_max:
network = str(information[0]) + '.' + str(num) + '.' + str(0) + '.' + str(0)
first_host = str(information[0]) + '.' + str(num) + '.' + str(0) + '.' + str(1)
last_host = str(information[0]) + '.' + str(span_max-1) + '.' + str(255) + '.' + str(254)
broadcast = str(information[0]) + '.' + str(span_max) + '.' + str(255) + '.' + str(255)
num += block_size
elif classB == True:
while num < 255:
if information[2] >= num:
span_max = num + block_size -1
if information[2] <= span_max:
network = str(information[0]) + '.' + str(information[1]) + '.' + str(num) + '.' + str(0)
first_host = str(information[0]) + '.' + str(information[1]) + '.' + str(num) + '.' + str(1)
last_host = str(information[0]) + '.' + str(information[1]) + '.' + str(span_max) + '.' + str(254)
broadcast = str(information[0]) + '.' + str(information[1]) + '.' + str(span_max) + '.' + str(255)
num += block_size
elif classC == True:
while num < 255:
if information[3] >= num:
span_max = num + block_size -1
if information[3] <= span_max:
network = str(information[0]) + '.' + str(information[1]) + '.' + str(information[2]) + '.' + str(num)
first_host = str(information[0]) + '.' + str(information[1]) + '.' + str(information[2]) + '.' + str(num + 1)
last_host = str(information[0]) + '.' + str(information[1]) + '.' + str(information[2]) + '.' + str(span_max -1)
broadcast = str(information[0]) + '.' + str(information[1]) + '.' + str(information[2]) + '.' + str(span_max)
new_cider = str(new_cider)
num += block_size
print('\n' * 70)
print(yellow + 'Network: ' + network + '/' + cider0)
print(green + 'First Host: ' + first_host + '/' + cider0)
print(light_blue + 'Last Host: ' + last_host + '/' + cider0)
print(purple + 'Broadcast: ' + broadcast + '/' + cider0)
print(white)
def calculate(self, v):
pass
list1 = []
list2 = []
s = subnet()
list1 = s.convert(ip_address)
block_size = s.get_block_size(list1[4])
s.get_network_span(list1, block_size)
for i in list1:
list2.append(s.binary(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment