Guest User

Untitled

a guest
Feb 21st, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. from ipaddress import ip_network
  2. import tqdm
  3. tqdm.tqdm.monitor_interval = 0
  4.  
  5.  
  6. class OverLapDetector:
  7. def __init__(self, subnet_list="subnet_list_2", max_prefix=18):
  8. self.base_list = []
  9. self.list_b = []
  10. self.subnet_list = subnet_list
  11. self.max_prefix = max_prefix
  12.  
  13. def detect_overlap(self):
  14. cleaned, dropped = self.sanitizer()
  15. self._write_conflict_list(self._compare_list(self.base_list, cleaned))
  16.  
  17. def sanitizer(self):
  18. self._readfiles("base_list", "subnet_list_2")
  19. deduped, dropped = self._dedup(self.list_b)
  20. return deduped, dropped
  21.  
  22. # TODO def _check_for_valid_ip(self):
  23.  
  24. # TODO def _check_for_large_subnets(self,max_prefix):
  25.  
  26. @staticmethod
  27. def _dedup(subnet_dedup):
  28. dropped_subnets = [] # Need to add for dropped file.
  29. print("Checking submitted subnets for overlaps and duplicates...")
  30. print(f"Number of subnets submitted: {len(subnet_dedup)}")
  31. starting_size = len(subnet_dedup)
  32. bar = tqdm.tqdm()
  33. bar.total = len(subnet_dedup)
  34. bar.mininterval = 0
  35. for i in range(0, len(subnet_dedup)):
  36. bar.update(1)
  37. for x in subnet_dedup[i+1:]:
  38. # print(f"{subnet_dedup[i]} {x}")
  39. if (ip_network(str(subnet_dedup[i]).strip('\n'), strict=False).overlaps(
  40. (ip_network(str(x).strip('\n'), strict=False)))):
  41. # print(f"\nSubnet {str(subnet_dedup[i]).strip()} overlaps with Subnet {str(x).strip()}")
  42. if subnet_dedup[i].split(sep='/')[1] > x.split(sep='/')[1]:
  43. # print(f"Dropping subnet {str(x).strip()}")
  44. dropped_subnets.append(x)
  45. subnet_dedup.remove(x)
  46. # t.update(1)
  47. # t.total=len(subnet_dedup)
  48.  
  49. else:
  50. # print(f"Dropping Subnet {str(subnet_dedup[i]).strip()}")
  51. subnet_dedup.remove(subnet_dedup[i])
  52. dropped_subnets.append(subnet_dedup[i])
  53. # t.update(1)
  54.  
  55. print("\n")
  56. print("*"*40)
  57. print(f"{starting_size-len(subnet_dedup)} overlapping subnets were found in the acquisition")
  58. print(f"Number of remaining subnets to compared: {len(subnet_dedup)}")
  59. # for i in subnet_dedup:
  60. # print(i,end="") subnet list."
  61. # f"\nLarger prefixes were dropped.")
  62. # f = open("base_list","w")
  63. # f.writelines(subnet_dedup)
  64. return subnet_dedup, dropped_subnets
  65.  
  66. @staticmethod
  67. def _write_conflict_list(conflict_list):
  68. try:
  69. with open("conflict_list.txt", "w") as f:
  70. f.writelines(conflict_list)
  71. except OSError as err:
  72. print(f"Error writing conflict list file. Error {err}")
  73.  
  74. @staticmethod
  75. def _compare_list(base_list, list_b):
  76. print("*"*40)
  77. print("Comparing submitted subnets to known company subnets...")
  78. conflict_list = []
  79. list_size = len(base_list)
  80. bar = tqdm.tqdm()
  81. bar.total = list_size
  82. bar.mininterval = 0
  83. print(f"There are {list_size} Labcorp subnets to compare to {len(list_b)} acquisition subnets.")
  84.  
  85. for i in base_list:
  86. bar.update(1)
  87.  
  88. for x in list_b:
  89.  
  90. if (ip_network(str(i).strip('\n'), strict=False).overlaps(
  91. (ip_network(str(x).strip('\n'), strict=False)))):
  92. # print(f"Line Number {count} List A IP {str(i).strip()} overlaps with List B IP {str(x).strip()}")
  93. conflict_list.append(f"Labcorp Subnet {str(i).strip()} "
  94. f"overlaps with acquisition subnet {str(x).strip()}\n")
  95.  
  96. print(f"\nTotal number of conflicting subnets is {len(conflict_list)}.")
  97. print("See conflict_list.txt for details")
  98. return conflict_list
  99.  
  100. def _readfiles(self, filename_a, filename_b):
  101. try:
  102. with open(filename_a, "r") as f:
  103. self.base_list = f.readlines()
  104. except OSError as err:
  105. print(f"Error opening file {filename_a}. Error: {err}")
  106. exit(1)
  107.  
  108. try:
  109. with open(filename_b, "r") as f:
  110. self.list_b = f.readlines()
  111. except OSError as err:
  112. print(f"Error opening file {filename_b}. Error: {err}")
  113. exit(1)
  114.  
  115.  
  116. t = OverLapDetector()
  117. t.detect_overlap()
  118. # t.sanitizer()
  119.  
  120. # if subnet_dedup[i].split(sep='/')[1] > x.split(sep='/')[1]:
  121. # if subnet_dedup[i] not in temp_list:
  122. # temp_list.append(subnet_dedup[i])
  123. # print("First")
  124. # if x in temp_list:
  125. # temp_list.remove(x)
  126. # else:
  127. # if x not in temp_list:
  128. # temp_list.append(x)
  129. # if subnet_dedup[i] in temp_list:
  130. # temp_list.remove(subnet_dedup[i])
  131. # print("Second")
  132. # else:
  133. # if subnet_dedup[i] not in temp_list:
  134. # temp_list.append(subnet_dedup[i])
  135. # print("Third")
  136. # print(f"Ending Length: {len(temp_list)}")
  137. # print("*" * 40)
  138. # for i in temp_list:
  139. # print(i, end="")
  140. # self._dedup(temp_list)
Add Comment
Please, Sign In to add comment