Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. ################# APP #1 - Part #1: IP and subnet check ######################
  2.  
  3. import random
  4. import sys
  5.  
  6. def sn_check(elements,pool):
  7. '''Checks the octets of the mask'''
  8. valid = True
  9. for each_one in elements:
  10. if int(each_one) not in pool:
  11. valid = False
  12. return valid
  13.  
  14.  
  15. print '\n'
  16.  
  17. #Check IP address validity:
  18. #a) Check for proper length (4 fields separated by dot)
  19. #b) Check for multicast (224), loopback (127) and link local (169.254)
  20. #c) Check for fields to be 0 <= x <= 255
  21.  
  22. while True:
  23. ip_add = raw_input(' Enter the IP address: ')
  24.  
  25. #Split into 4 octets
  26. ip_occ = ip_add.split('.')
  27.  
  28.  
  29. 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) ) :
  30.  
  31. #print ('\n IP address validity check PASSED! \n')
  32. break
  33.  
  34. else:
  35. print '\n The IP address is not VALID! Please try again \n'
  36. continue
  37.  
  38.  
  39. while True:
  40.  
  41.  
  42. sn_msk = raw_input(' Enter the SN mask: ')
  43.  
  44. #split subnet into 4 octets
  45.  
  46. sn_occ = sn_msk.split('.')
  47. octets = [0,128,192,224,240,248,252,254,255]
  48.  
  49. 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])):
  50.  
  51. #print ('\n Subnet mask validity check PASSED! \n')
  52. break
  53.  
  54. else:
  55. print '\n The subnet mask is INVALID! Please retry! \n'
  56. continue
  57.  
  58. ################# APP #1 - Part #2: Subnet compute, nr. of hosts, wildcard ######################
  59.  
  60.  
  61. #list that holds the Network Address octets (still separated though)
  62. net_occ = []
  63.  
  64. #compute the logical AND between IP_add and SN_mask and store the octets in the net_occ list
  65. for index in range(0,4):
  66. net_occ.append(int(ip_occ[index]) & int(sn_occ[index]))
  67.  
  68. #convert the Network Address octets list into a string & strip [,[,. and spaces
  69. net_addr = str(net_occ).replace('[','').replace(']','').replace(',','.').replace(' ','')
  70.  
  71. print '\n - The actual subnet is: ', net_addr + '\n'
  72.  
  73. #list that holds the Wildcard Mask octets (still separated though)
  74. w_occ = []
  75.  
  76. #compute the wildcard mask by substracting each SN_mask octet from 255
  77. for each_octet in sn_msk.split('.'):
  78. w_occ.append(255-int(each_octet))
  79.  
  80. #convert Wildcard mask to a string and strip [,],. and spaces
  81. w_card = str(w_occ).replace('[','').replace(']','').replace(',','.').replace(' ','')
  82.  
  83. print ' - The wildcard mask is: ', str(w_card) + '\n'
  84.  
  85. all_zeroes = 0
  86.  
  87. for each in sn_occ:
  88. each = int(each)
  89. bin_oct = bin(each).split('b')[1].zfill(8)
  90. zero_count = bin_oct.count('0')
  91. all_zeroes+=zero_count
  92.  
  93. hosts = 2**all_zeroes - 2
  94.  
  95. print ' - Total number of usable hosts: ', str(hosts) + '\n'
  96.  
  97.  
  98. ################# APP #1 - Part #3: The broadcast address ######################
  99.  
  100. #init 2 lists for SN and IP converted to binary octet format; we'll be doing work at bit level
  101. binary_msk = []
  102. binary_net = []
  103.  
  104. #convert the network address to a full 32-bit string
  105. for each in net_occ:
  106. each = int(each)
  107. bin_oct = bin(each).split('b')[1].zfill(8)
  108. binary_net.append(bin_oct)
  109.  
  110. #convert the subnet mask to a full 32-bit string
  111. for each in sn_occ:
  112. each = int(each)
  113. bin_oct = bin(each).split('b')[1].zfill(8)
  114. binary_msk.append(bin_oct)
  115.  
  116. #convert SN mask and IP address from a list of 8-bit fields to a string and strip out parantheses, ghilimele & replace , with .
  117. binary_net = str(binary_net).replace('[','').replace(']','').replace(',','.').replace(' ','').replace('\'','')
  118. binary_msk = str(binary_msk).replace('[','').replace(']','').replace(',','.').replace(' ','').replace('\'','')
  119.  
  120. #print binary_msk
  121. #print binary_net
  122.  
  123. #create 2 lists, each with 4 elements (elements are actually the octets)
  124. binary_net = binary_net.split('.')
  125. binary_msk = binary_msk.split('.')
  126.  
  127. #init the broadcast address as a list of lists so that we can modify zeroes and ones - str types won't allow this :(
  128. binary_bst = [list(each) for each in binary_net] #would have been easier if strings allowed it though
  129.  
  130. #print binary_bst
  131. #print binary_net
  132. #print binary_msk
  133.  
  134. #go through the binary subnet mask - if a 0 is encountered then change the binary_net value to a 1
  135. for i in range(0,4):
  136. #because we have 4 octet elements per each list
  137. for j in range(0,8):
  138. #because each octet has 8 bits - duhh!! called octet :P
  139. if binary_msk[i][j] == '0':
  140. binary_bst[i][j] = '1'
  141.  
  142. decimal_bst = []
  143.  
  144. for each in range(0,len(binary_bst)):
  145. binary_bst[each] = str(binary_bst[each]).replace('[','').replace(']','').replace(',','').replace(' ','').replace('\'','')
  146. decimal_bst.append(int(binary_bst[each],2))
  147.  
  148. #print decimal_bst
  149. #print binary_bst
  150.  
  151. decimal_bst = str(decimal_bst).replace('[','').replace(']','').replace(',','.').replace(' ','')
  152. print ' - The Broadcast IP address is: ', decimal_bst + '\n'
  153.  
  154. ################# APP #1 - Part #3: Generate the random IP address ######################
  155.  
  156. #print binary_net
  157. #print binary_bst
  158.  
  159. while True:
  160.  
  161. user_choice = raw_input('Generate a random IP address in this range(Y/N)?: ')
  162.  
  163. if user_choice == 'Y':
  164.  
  165. binary_random_add = binary_net #these are lists
  166.  
  167. for i in range(0,4):
  168. if binary_net[i] != binary_bst[i]:
  169. random_octet = random.randrange(int(binary_net[i],2),int(binary_bst[i],2))
  170. binary_random_add[i] = bin(random_octet).split('b')[1].zfill(8)
  171. #del random_octet
  172.  
  173. binary_random_add = [str(int(binary_random_add[i],2)) for i in range(0,4)]
  174. #print binary_random_add
  175.  
  176. decimal_random_add = '.'.join(binary_random_add)
  177. print ' - The random IP address is: ' + decimal_random_add + '\n'
  178. del decimal_random_add
  179.  
  180. elif user_choice == 'N':
  181. print 'Application closing down as requested. BYE.'
  182. break
  183. else:
  184. print '\n INVALID input detected! Please try again \n'
  185. continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement