Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ipaddress import ip_network
- import tqdm
- tqdm.tqdm.monitor_interval = 0
- class OverLapDetector:
- def __init__(self, subnet_list="subnet_list_2", max_prefix=18):
- self.base_list = []
- self.list_b = []
- self.subnet_list = subnet_list
- self.max_prefix = max_prefix
- def detect_overlap(self):
- cleaned, dropped = self.sanitizer()
- self._write_conflict_list(self._compare_list(self.base_list, cleaned))
- def sanitizer(self):
- self._readfiles("base_list", "subnet_list_2")
- deduped, dropped = self._dedup(self.list_b)
- return deduped, dropped
- # TODO def _check_for_valid_ip(self):
- # TODO def _check_for_large_subnets(self,max_prefix):
- @staticmethod
- def _dedup(subnet_dedup):
- dropped_subnets = [] # Need to add for dropped file.
- print("Checking submitted subnets for overlaps and duplicates...")
- print(f"Number of subnets submitted: {len(subnet_dedup)}")
- starting_size = len(subnet_dedup)
- bar = tqdm.tqdm()
- bar.total = len(subnet_dedup)
- bar.mininterval = 0
- for i in range(0, len(subnet_dedup)):
- bar.update(1)
- for x in subnet_dedup[i+1:]:
- # print(f"{subnet_dedup[i]} {x}")
- if (ip_network(str(subnet_dedup[i]).strip('\n'), strict=False).overlaps(
- (ip_network(str(x).strip('\n'), strict=False)))):
- # print(f"\nSubnet {str(subnet_dedup[i]).strip()} overlaps with Subnet {str(x).strip()}")
- if subnet_dedup[i].split(sep='/')[1] > x.split(sep='/')[1]:
- # print(f"Dropping subnet {str(x).strip()}")
- dropped_subnets.append(x)
- subnet_dedup.remove(x)
- # t.update(1)
- # t.total=len(subnet_dedup)
- else:
- # print(f"Dropping Subnet {str(subnet_dedup[i]).strip()}")
- subnet_dedup.remove(subnet_dedup[i])
- dropped_subnets.append(subnet_dedup[i])
- # t.update(1)
- print("\n")
- print("*"*40)
- print(f"{starting_size-len(subnet_dedup)} overlapping subnets were found in the acquisition")
- print(f"Number of remaining subnets to compared: {len(subnet_dedup)}")
- # for i in subnet_dedup:
- # print(i,end="") subnet list."
- # f"\nLarger prefixes were dropped.")
- # f = open("base_list","w")
- # f.writelines(subnet_dedup)
- return subnet_dedup, dropped_subnets
- @staticmethod
- def _write_conflict_list(conflict_list):
- try:
- with open("conflict_list.txt", "w") as f:
- f.writelines(conflict_list)
- except OSError as err:
- print(f"Error writing conflict list file. Error {err}")
- @staticmethod
- def _compare_list(base_list, list_b):
- print("*"*40)
- print("Comparing submitted subnets to known company subnets...")
- conflict_list = []
- list_size = len(base_list)
- bar = tqdm.tqdm()
- bar.total = list_size
- bar.mininterval = 0
- print(f"There are {list_size} Labcorp subnets to compare to {len(list_b)} acquisition subnets.")
- for i in base_list:
- bar.update(1)
- for x in list_b:
- if (ip_network(str(i).strip('\n'), strict=False).overlaps(
- (ip_network(str(x).strip('\n'), strict=False)))):
- # print(f"Line Number {count} List A IP {str(i).strip()} overlaps with List B IP {str(x).strip()}")
- conflict_list.append(f"Labcorp Subnet {str(i).strip()} "
- f"overlaps with acquisition subnet {str(x).strip()}\n")
- print(f"\nTotal number of conflicting subnets is {len(conflict_list)}.")
- print("See conflict_list.txt for details")
- return conflict_list
- def _readfiles(self, filename_a, filename_b):
- try:
- with open(filename_a, "r") as f:
- self.base_list = f.readlines()
- except OSError as err:
- print(f"Error opening file {filename_a}. Error: {err}")
- exit(1)
- try:
- with open(filename_b, "r") as f:
- self.list_b = f.readlines()
- except OSError as err:
- print(f"Error opening file {filename_b}. Error: {err}")
- exit(1)
- t = OverLapDetector()
- t.detect_overlap()
- # t.sanitizer()
- # if subnet_dedup[i].split(sep='/')[1] > x.split(sep='/')[1]:
- # if subnet_dedup[i] not in temp_list:
- # temp_list.append(subnet_dedup[i])
- # print("First")
- # if x in temp_list:
- # temp_list.remove(x)
- # else:
- # if x not in temp_list:
- # temp_list.append(x)
- # if subnet_dedup[i] in temp_list:
- # temp_list.remove(subnet_dedup[i])
- # print("Second")
- # else:
- # if subnet_dedup[i] not in temp_list:
- # temp_list.append(subnet_dedup[i])
- # print("Third")
- # print(f"Ending Length: {len(temp_list)}")
- # print("*" * 40)
- # for i in temp_list:
- # print(i, end="")
- # self._dedup(temp_list)
Add Comment
Please, Sign In to add comment