Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ################# APP #1 - Part #1: IP and subnet check ######################
- import random
- import sys
- def sn_check(elements,pool):
- '''Checks the octets of the mask'''
- valid = True
- for each_one in elements:
- if int(each_one) not in pool:
- valid = False
- return valid
- print '\n'
- #Check IP address validity:
- #a) Check for proper length (4 fields separated by dot)
- #b) Check for multicast (224), loopback (127) and link local (169.254)
- #c) Check for fields to be 0 <= x <= 255
- while True:
- ip_add = raw_input(' Enter the IP address: ')
- #Split into 4 octets
- ip_occ = ip_add.split('.')
- if (len(ip_occ) == 4) and (int(ip_occ[0]) != 127) and (1 <= int(ip_occ[0]) <= 223) and (str(ip_occ[0]+'.'+ ip_occ[1]) != '169.254') and ((0,0,0,0) <= (int(ip_occ[0]),int(ip_occ[1]),int(ip_occ[2]),int(ip_occ[3])) <= (255,255,255,255) ) :
- #print ('\n IP address validity check PASSED! \n')
- break
- else:
- print '\n The IP address is not VALID! Please try again \n'
- continue
- while True:
- sn_msk = raw_input(' Enter the SN mask: ')
- #split subnet into 4 octets
- sn_occ = sn_msk.split('.')
- octets = [0,128,192,224,240,248,252,254,255]
- if (len(sn_occ) == 4) and (int(sn_occ[0]) == 255) and (sn_check(sn_occ,octets)) and (int(sn_occ[0]) >= int(sn_occ[1]) >= int(sn_occ[2]) >= int(sn_occ[3])):
- #print ('\n Subnet mask validity check PASSED! \n')
- break
- else:
- print '\n The subnet mask is INVALID! Please retry! \n'
- continue
- ################# APP #1 - Part #2: Subnet compute, nr. of hosts, wildcard ######################
- #list that holds the Network Address octets (still separated though)
- net_occ = []
- #compute the logical AND between IP_add and SN_mask and store the octets in the net_occ list
- for index in range(0,4):
- net_occ.append(int(ip_occ[index]) & int(sn_occ[index]))
- #convert the Network Address octets list into a string & strip [,[,. and spaces
- net_addr = str(net_occ).replace('[','').replace(']','').replace(',','.').replace(' ','')
- print '\n - The actual subnet is: ', net_addr + '\n'
- #list that holds the Wildcard Mask octets (still separated though)
- w_occ = []
- #compute the wildcard mask by substracting each SN_mask octet from 255
- for each_octet in sn_msk.split('.'):
- w_occ.append(255-int(each_octet))
- #convert Wildcard mask to a string and strip [,],. and spaces
- w_card = str(w_occ).replace('[','').replace(']','').replace(',','.').replace(' ','')
- print ' - The wildcard mask is: ', str(w_card) + '\n'
- all_zeroes = 0
- for each in sn_occ:
- each = int(each)
- bin_oct = bin(each).split('b')[1].zfill(8)
- zero_count = bin_oct.count('0')
- all_zeroes+=zero_count
- hosts = 2**all_zeroes - 2
- print ' - Total number of usable hosts: ', str(hosts) + '\n'
- ################# APP #1 - Part #3: The broadcast address ######################
- #init 2 lists for SN and IP converted to binary octet format; we'll be doing work at bit level
- binary_msk = []
- binary_net = []
- #convert the network address to a full 32-bit string
- for each in net_occ:
- each = int(each)
- bin_oct = bin(each).split('b')[1].zfill(8)
- binary_net.append(bin_oct)
- #convert the subnet mask to a full 32-bit string
- for each in sn_occ:
- each = int(each)
- bin_oct = bin(each).split('b')[1].zfill(8)
- binary_msk.append(bin_oct)
- #convert SN mask and IP address from a list of 8-bit fields to a string and strip out parantheses, ghilimele & replace , with .
- binary_net = str(binary_net).replace('[','').replace(']','').replace(',','.').replace(' ','').replace('\'','')
- binary_msk = str(binary_msk).replace('[','').replace(']','').replace(',','.').replace(' ','').replace('\'','')
- #print binary_msk
- #print binary_net
- #create 2 lists, each with 4 elements (elements are actually the octets)
- binary_net = binary_net.split('.')
- binary_msk = binary_msk.split('.')
- #init the broadcast address as a list of lists so that we can modify zeroes and ones - str types won't allow this :(
- binary_bst = [list(each) for each in binary_net] #would have been easier if strings allowed it though
- #print binary_bst
- #print binary_net
- #print binary_msk
- #go through the binary subnet mask - if a 0 is encountered then change the binary_net value to a 1
- for i in range(0,4):
- #because we have 4 octet elements per each list
- for j in range(0,8):
- #because each octet has 8 bits - duhh!! called octet :P
- if binary_msk[i][j] == '0':
- binary_bst[i][j] = '1'
- decimal_bst = []
- for each in range(0,len(binary_bst)):
- binary_bst[each] = str(binary_bst[each]).replace('[','').replace(']','').replace(',','').replace(' ','').replace('\'','')
- decimal_bst.append(int(binary_bst[each],2))
- #print decimal_bst
- #print binary_bst
- decimal_bst = str(decimal_bst).replace('[','').replace(']','').replace(',','.').replace(' ','')
- print ' - The Broadcast IP address is: ', decimal_bst + '\n'
- ################# APP #1 - Part #3: Generate the random IP address ######################
- #print binary_net
- #print binary_bst
- while True:
- user_choice = raw_input('Generate a random IP address in this range(Y/N)?: ')
- if user_choice == 'Y':
- binary_random_add = binary_net #these are lists
- for i in range(0,4):
- if binary_net[i] != binary_bst[i]:
- random_octet = random.randrange(int(binary_net[i],2),int(binary_bst[i],2))
- binary_random_add[i] = bin(random_octet).split('b')[1].zfill(8)
- #del random_octet
- binary_random_add = [str(int(binary_random_add[i],2)) for i in range(0,4)]
- #print binary_random_add
- decimal_random_add = '.'.join(binary_random_add)
- print ' - The random IP address is: ' + decimal_random_add + '\n'
- del decimal_random_add
- elif user_choice == 'N':
- print 'Application closing down as requested. BYE.'
- break
- else:
- print '\n INVALID input detected! Please try again \n'
- continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement