1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

calculate first and last host instead of using the slower iterable

This commit is contained in:
Kelly Brazil
2022-07-27 13:08:35 -07:00
parent d970b435b4
commit 11d2eb35be

View File

@ -102,22 +102,14 @@ def parse(
network_string = str(interface.network).split('/')[0] network_string = str(interface.network).split('/')[0]
network_cidr = int(str(interface.with_prefixlen).split('/')[1]) network_cidr = int(str(interface.with_prefixlen).split('/')[1])
network = ipaddress.ip_network(f'{network_string}/{network_cidr}') network = ipaddress.ip_network(f'{network_string}/{network_cidr}')
broadcast_string = str(network.broadcast_address)
bare_ip_string = str(interface.ip) bare_ip_string = str(interface.ip)
bare_ip = ipaddress.ip_address(bare_ip_string) bare_ip = ipaddress.ip_address(bare_ip_string)
ip_ptr = bare_ip.reverse_pointer ip_ptr = bare_ip.reverse_pointer
first_host = str(next(network.hosts())) # TODO: fix for /30, /31, /32, etc.
first_host = str(ipaddress.ip_address(network_string) + 1)
# hack to speed up iterating through large ipv6 subnets last_host = str(ipaddress.ip_address(broadcast_string) - 1)
# only do last_host for masks >= /16 (ipv4) or >= /120 (ipv6)
last_host = None
if any((
int(interface.version) == 4 and network_cidr >= 16,
int(interface.version) == 6 and network_cidr >= 120,
)):
dd = deque(network.hosts(), maxlen=1)
last_host = str(dd.pop())
raw_output = { raw_output = {
'version': int(interface.version), 'version': int(interface.version),
@ -127,12 +119,12 @@ def parse(
'ip_exploded': str(interface.exploded), 'ip_exploded': str(interface.exploded),
'dns_ptr': ip_ptr, 'dns_ptr': ip_ptr,
'network': network_string, 'network': network_string,
'broadcast': str(network.broadcast_address), 'broadcast': broadcast_string,
'hostmask': str(interface.with_hostmask).split('/')[1], 'hostmask': str(interface.with_hostmask).split('/')[1],
'netmask': str(interface.with_netmask).split('/')[1], 'netmask': str(interface.with_netmask).split('/')[1],
'cidr_netmask': network_cidr, 'cidr_netmask': network_cidr,
'first_host': first_host, 'first_host': first_host,
'last_host': last_host, # None if netmask is too small 'last_host': last_host,
'is_multicast': interface.is_multicast, 'is_multicast': interface.is_multicast,
'is_private': interface.is_private, 'is_private': interface.is_private,
'is_global': interface.is_global, 'is_global': interface.is_global,