|
1 | 1 | import ipaddress |
2 | 2 | import argparse |
| 3 | +import os |
3 | 4 |
|
4 | 5 | def count_valid_hosts_from_file(file_path): |
5 | 6 | total_hosts = 0 |
6 | 7 |
|
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 |
12 | 14 |
|
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}") |
26 | 29 |
|
27 | 30 | return total_hosts |
28 | 31 |
|
29 | 32 | def main(): |
30 | 33 | 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.") |
32 | 36 | args = parser.parse_args() |
33 | 37 |
|
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() |
36 | 54 |
|
37 | 55 | if __name__ == "__main__": |
38 | 56 | main() |
0 commit comments