Skip to content

Commit 99ec17f

Browse files
authored
Merge pull request #1 from DefensiveOrigins/shopdupes
show dupes
2 parents 1bd7cd3 + 2f3673f commit 99ec17f

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

HostCount.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ipaddress
22
import argparse
33
import os
4+
from collections import defaultdict
45

56
def extract_unique_hosts_from_file(file_path):
67
unique_hosts = set()
@@ -34,19 +35,41 @@ def main():
3435
parser = argparse.ArgumentParser(description="Count unique valid hosts from CIDRs or IPs in files.")
3536
parser.add_argument("file", nargs='?', help="Path to file containing CIDR ranges or IPs, one per line.")
3637
parser.add_argument("-d", "--directory", help="Directory of files to process.")
38+
parser.add_argument("-D", "--duplicates", action="store_true",
39+
help="When processing a directory, list hosts that appear in more than one file and which files they appear in.")
3740
args = parser.parse_args()
3841

3942
if args.directory:
4043
grand_total_hosts = set()
44+
file_hosts_map = {}
4145
print(f"\nProcessing directory: {args.directory}")
42-
for entry in os.listdir(args.directory):
46+
for entry in sorted(os.listdir(args.directory)):
4347
full_path = os.path.join(args.directory, entry)
4448
if os.path.isfile(full_path):
4549
file_hosts = extract_unique_hosts_from_file(full_path)
50+
file_hosts_map[entry] = file_hosts
4651
print(f"{entry}: {len(file_hosts)} unique hosts")
4752
grand_total_hosts.update(file_hosts)
4853
print(f"\nTotal unique hosts across all files: {len(grand_total_hosts)}")
4954

55+
if args.duplicates:
56+
# Build host -> set(files) mapping
57+
host_to_files = defaultdict(set)
58+
for fname, hosts in file_hosts_map.items():
59+
for h in hosts:
60+
host_to_files[h].add(fname)
61+
62+
# Find hosts present in more than one file
63+
duplicates = {host: sorted(list(files)) for host, files in host_to_files.items() if len(files) > 1}
64+
65+
if not duplicates:
66+
print("\nNo duplicate hosts found between files.")
67+
else:
68+
print(f"\nDuplicate hosts found between files: {len(duplicates)} hosts\n")
69+
for host in sorted(duplicates.keys(), key=lambda x: tuple(int(p) for p in x.split('.'))):
70+
files_list = ", ".join(duplicates[host])
71+
print(f"{host}: {files_list}")
72+
5073
elif args.file:
5174
unique_hosts = extract_unique_hosts_from_file(args.file)
5275
print(f"\nTotal unique hosts in {args.file} (excluding network and broadcast): {len(unique_hosts)}")

0 commit comments

Comments
 (0)