-
Notifications
You must be signed in to change notification settings - Fork 4
/
subnetting.py
52 lines (42 loc) · 1.51 KB
/
subnetting.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
#!usr/bin/env python3
# -*- coding: utf-8 -*-
from ipaddress import AddressValueError, IPv4Network, NetmaskValueError
from typing import Any, Dict, List
from rich import print
def subnetting(input_subnets: List[Dict], gateway: int) -> List[Dict[str, Any]]:
"""Does subnetting CIDR Notation
Parameters
----------
input_subnets : List[Dict]
Input subnets in CSV file
gateway : int
The selected Gateway
Returns
-------
List[Dict[str, Any]]
Result of subnetting
Raises
------
SystemExit
AddressValueError, NetmaskValueError, IndexError
"""
try:
# Loop over input_subnets
for subnet in input_subnets:
network = IPv4Network(address=subnet["cidr"])
hosts = list(network.hosts())
yield {
"cidr": str(network),
"net_addr": str(network.network_address),
"prfx_len": str(network.prefixlen),
"brdcst_addr": str(network.broadcast_address),
"netmask": str(network.netmask),
"wildmask": str(network.hostmask),
"num_hosts": network.num_addresses - 2,
"range": str(hosts[0])
if hosts[0] == hosts[-1]
else f"{hosts[0]} → {hosts[-1]}",
"gateway": str(hosts[0]) if gateway else str(hosts[-1]),
}
except (AddressValueError, NetmaskValueError, IndexError) as e:
raise SystemExit(print(f"[red]subnetting.py: {e}")) from e