Skip to content

Commit e4b2111

Browse files
authored
Update HostCount.py
add option to specify directory of files
1 parent 08777a2 commit e4b2111

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

HostCount.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,56 @@
11
import ipaddress
22
import argparse
3+
import os
34

45
def count_valid_hosts_from_file(file_path):
56
total_hosts = 0
67

7-
with open(file_path, 'r') as f:
8-
for line_number, line in enumerate(f, start=1):
9-
entry = line.strip()
10-
if not entry or entry.startswith("#"):
11-
continue # Skip empty lines or comments
8+
try:
9+
with open(file_path, 'r') as f:
10+
for line_number, line in enumerate(f, start=1):
11+
entry = line.strip()
12+
if not entry or entry.startswith("#"):
13+
continue # Skip empty lines or comments
1214

13-
try:
14-
# Treat plain IPs as /32
15-
if '/' not in entry:
16-
entry += '/32'
17-
network = ipaddress.ip_network(entry, strict=False)
18-
if isinstance(network, ipaddress.IPv4Network):
19-
if network.prefixlen == 32:
20-
total_hosts += 1
21-
else:
22-
num_hosts = max(network.num_addresses - 2, 0)
23-
total_hosts += num_hosts
24-
except ValueError as e:
25-
print(f"WARNING: Line {line_number} - Invalid entry '{entry}': {e}")
15+
try:
16+
if '/' not in entry:
17+
entry += '/32'
18+
network = ipaddress.ip_network(entry, strict=False)
19+
if isinstance(network, ipaddress.IPv4Network):
20+
if network.prefixlen == 32:
21+
total_hosts += 1
22+
else:
23+
num_hosts = max(network.num_addresses - 2, 0)
24+
total_hosts += num_hosts
25+
except ValueError as e:
26+
print(f"WARNING: {file_path} Line {line_number} - Invalid entry '{entry}': {e}")
27+
except Exception as e:
28+
print(f"ERROR: Could not read file {file_path}: {e}")
2629

2730
return total_hosts
2831

2932
def main():
3033
parser = argparse.ArgumentParser(description="Count valid hosts in a list of CIDRs or IPs.")
31-
parser.add_argument("file", help="Path to file containing CIDR ranges or IPs, one per line.")
34+
parser.add_argument("file", nargs='?', help="Path to file containing CIDR ranges or IPs, one per line.")
35+
parser.add_argument("-d", "--directory", help="Directory of files to process.")
3236
args = parser.parse_args()
3337

34-
total = count_valid_hosts_from_file(args.file)
35-
print(f"\nTotal valid hosts (excluding network and broadcast, except for /32 IPs): {total}")
38+
if args.directory:
39+
grand_total = 0
40+
print(f"\nProcessing directory: {args.directory}")
41+
for entry in os.listdir(args.directory):
42+
full_path = os.path.join(args.directory, entry)
43+
if os.path.isfile(full_path):
44+
count = count_valid_hosts_from_file(full_path)
45+
print(f"{entry}: {count} hosts")
46+
grand_total += count
47+
print(f"\nTotal hosts across all files: {grand_total}")
48+
elif args.file:
49+
total = count_valid_hosts_from_file(args.file)
50+
print(f"\nTotal valid hosts in {args.file} (excluding network and broadcast, except for /32 IPs): {total}")
51+
else:
52+
print("ERROR: Either a file or a directory must be specified.")
53+
parser.print_help()
3654

3755
if __name__ == "__main__":
3856
main()

0 commit comments

Comments
 (0)