Skip to content

Commit 609b0d1

Browse files
committed
rfc1918
1 parent 99ec17f commit 609b0d1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

HostCount.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
import os
44
from collections import defaultdict
55

6+
# RFC1918 networks
7+
RFC1918_NETWORKS = [
8+
ipaddress.ip_network("10.0.0.0/8"),
9+
ipaddress.ip_network("172.16.0.0/12"),
10+
ipaddress.ip_network("192.168.0.0/16"),
11+
]
12+
13+
def is_rfc1918(ip_str):
14+
try:
15+
ip = ipaddress.ip_address(ip_str)
16+
return any(ip in net for net in RFC1918_NETWORKS)
17+
except ValueError:
18+
return False
19+
620
def extract_unique_hosts_from_file(file_path):
721
unique_hosts = set()
822

@@ -37,6 +51,8 @@ def main():
3751
parser.add_argument("-d", "--directory", help="Directory of files to process.")
3852
parser.add_argument("-D", "--duplicates", action="store_true",
3953
help="When processing a directory, list hosts that appear in more than one file and which files they appear in.")
54+
parser.add_argument("-r", "--rfc1918", action="store_true",
55+
help="Optionally show how many hosts are in RFC1918 space vs not (per-file and totals).")
4056
args = parser.parse_args()
4157

4258
if args.directory:
@@ -52,6 +68,18 @@ def main():
5268
grand_total_hosts.update(file_hosts)
5369
print(f"\nTotal unique hosts across all files: {len(grand_total_hosts)}")
5470

71+
if args.rfc1918:
72+
print("\nRFC1918 breakdown per file:")
73+
for fname in sorted(file_hosts_map.keys()):
74+
hosts = file_hosts_map[fname]
75+
rfc_count = sum(1 for h in hosts if is_rfc1918(h))
76+
non_rfc = len(hosts) - rfc_count
77+
print(f"{fname}: {rfc_count} RFC1918, {non_rfc} non-RFC1918")
78+
79+
total_rfc = sum(1 for h in grand_total_hosts if is_rfc1918(h))
80+
total_non_rfc = len(grand_total_hosts) - total_rfc
81+
print(f"\nTotals across all files: {total_rfc} RFC1918, {total_non_rfc} non-RFC1918")
82+
5583
if args.duplicates:
5684
# Build host -> set(files) mapping
5785
host_to_files = defaultdict(set)
@@ -74,6 +102,11 @@ def main():
74102
unique_hosts = extract_unique_hosts_from_file(args.file)
75103
print(f"\nTotal unique hosts in {args.file} (excluding network and broadcast): {len(unique_hosts)}")
76104

105+
if args.rfc1918:
106+
rfc_count = sum(1 for h in unique_hosts if is_rfc1918(h))
107+
non_rfc = len(unique_hosts) - rfc_count
108+
print(f"{args.file}: {rfc_count} RFC1918, {non_rfc} non-RFC1918")
109+
77110
else:
78111
print("ERROR: Either a file or a directory must be specified.")
79112
parser.print_help()

0 commit comments

Comments
 (0)