fork download
  1. def getSubnetMask(ip):
  2. return int(ip[len(ip)-2:])
  3.  
  4.  
  5. def subnet_calculator(ip_list):
  6.  
  7. ip_ranges = []
  8. addresses = []
  9. for ip in ip_list:
  10.  
  11.  
  12. # find subnet mask
  13. subnet = getSubnetMask(ip)
  14.  
  15. # calculate subnet 2**(8-(subnet % 8)
  16. free_addresses = 2**(8-(int(subnet) % 8))
  17.  
  18. # remove dots in IP-Address so i have a number
  19. split_ip = ip[:len(ip)-3].split('.')
  20.  
  21. block = getBlock(subnet)
  22.  
  23. start_ip = list(split_ip)
  24. end_ip = list(split_ip)
  25. if free_addresses != 256:
  26. start_range = 0
  27. # find start and end of the IP-Range
  28. while True:
  29. end_range = start_range + free_addresses
  30. if start_range <= int(split_ip[block]) and end_range >= int(split_ip[block]):
  31. break
  32. start_range = end_range
  33.  
  34. # create two copy's of the split_ip list and change their start and end adress
  35.  
  36. start_ip[block] = str(start_range)
  37. end_ip[block] = str(end_range-1)
  38. elif subnet != 32:
  39. start_ip[block] = '0'
  40. end_ip[block] = '255'
  41.  
  42. #create 2d list and add start and end ip
  43. ip_ranges.append([])
  44. # save start and end in a 2D-list
  45. ip_ranges[-1].append(ip[:len(ip)-3])
  46. ip_ranges[-1].append(subnet)
  47. ip_ranges[-1].append('.'.join(start_ip))
  48. ip_ranges[-1].append('.'.join(end_ip))
  49.  
  50. addresses.append(ip[:len(ip)-3])
  51.  
  52.  
  53. for address, subnet, start, end in ip_ranges:
  54. ip_blocks = address.split('.')
  55. # ip_block = getBlock(subnet)
  56. for a, s, st, e in ip_ranges:
  57. if address != a and subnet >= s:
  58. start_block = st.split('.')
  59. end_block = e.split('.')
  60. even = True
  61. compare_block = getBlock(s)
  62. for i in reversed(range(compare_block)):
  63. if ip_blocks[i] != start_block[i]:
  64. even = False
  65. break
  66.  
  67. # if ip in range -> remove from list
  68. if int(start_block[compare_block]) <= int(ip_blocks[compare_block]) and int(end_block[compare_block]) >= int(ip_blocks[compare_block]) and even:
  69. addresses.remove(address)
  70. break
  71.  
  72.  
  73. for ip in addresses:
  74. print(ip)
  75.  
  76. print('--------------')
  77.  
  78.  
  79.  
  80. def getBlock(subnet):
  81. if subnet >= 0 and subnet <= 7:
  82. return 0
  83. elif subnet >= 8 and subnet <= 15:
  84. return 1
  85. elif subnet >= 16 and subnet <= 23:
  86. return 2
  87. else:
  88. return 3
  89.  
  90. subnet_calculator(['172.26.32.162/32', '172.26.32.0/24', '172.26.0.0/16', '172.26.32.199/25'])
  91. subnet_calculator(['192.168.0.0/16', '172.24.96.17/32', '172.50.137.225/32', '202.139.219.192/32', '172.24.68.0/24',
  92. '192.183.125.71/32', '201.45.111.138/32', '192.168.59.211/32', '192.168.26.13/32', '172.24.0.0/17',
  93. '172.24.5.1/32', '172.24.68.37/32', '172.24.168.32/32'])
  94.  
  95. subnet_calculator(['172.24.68.0/24', '172.24.0.0/17', '192.168.59.211/32', '192.168.0.0/16'])
  96. subnet_calculator(['172.24.96.17/32', '172.24.0.0/17'])
Success #stdin #stdout 0s 23352KB
stdin
Standard input is empty
stdout
172.26.0.0
--------------
192.168.0.0
172.50.137.225
202.139.219.192
192.183.125.71
201.45.111.138
172.24.0.0
172.24.168.32
--------------
172.24.0.0
192.168.0.0
--------------
172.24.0.0
--------------