-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipenum.py
66 lines (59 loc) · 1.95 KB
/
ipenum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Enumerates the individual IP addresses for any given subnet
Author: Boabdil Perez
Date: 9 August 2023
Version: 0.1.0
Args:
addresses: Any number of IP addresses in CIDR notation
Returns:
A list of IP addresses that comprises the entirety of the network for each address passed in.
Raises:
ValueError: If the provided string is not a valid IPv4 address
"""
import argparse
import json
from ipaddress import IPv4Interface
from sys import stderr
def main(args):
"""Script logic entrypoint"""
valid_addrs: list[IPv4Interface] = []
json_ret: dict[str, str] = {}
json_flag: bool = args.json_flag
for address in args.addresses:
try:
valid_addrs.append(IPv4Interface(address))
json_ret.update({str(IPv4Interface(address).network): list()})
except ValueError as err:
print(f"{address} is not a valid IPv4 address: {err=}", file=stderr)
for address in valid_addrs:
if not json_flag:
print(f"Address: {address}\r\nNetwork: {address.network}")
print("\r\n===== IP Address List Start =====")
for unique_ip in address.network:
if not json_flag:
print(str(unique_ip))
json_ret[str(address.network)].append(str(unique_ip))
if not json_flag:
print("===== End IP Address List =====\r\n")
if json_flag:
json_output = json.dumps(json_ret, indent=2)
print(json_output)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Enumerate the entire network behind any IP address"
)
parser.add_argument(
"addresses",
metavar="A",
type=str,
nargs="+",
help="One or more IP addresses in CIDR notation",
)
parser.add_argument(
"--json",
dest="json_flag",
action="store_true",
default=False,
help="Return answer in json format",
)
arguments = parser.parse_args()
main(arguments)