mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
do str/int conversions up front
This commit is contained in:
@ -27,7 +27,7 @@ Examples:
|
|||||||
$ ip_address | jc --ip_address -p -r
|
$ ip_address | jc --ip_address -p -r
|
||||||
[]
|
[]
|
||||||
"""
|
"""
|
||||||
from typing import List, Dict
|
from typing import Dict
|
||||||
import binascii
|
import binascii
|
||||||
import ipaddress
|
import ipaddress
|
||||||
from collections import deque
|
from collections import deque
|
||||||
@ -100,21 +100,21 @@ def parse(
|
|||||||
|
|
||||||
interface = ipaddress.ip_interface(data.strip())
|
interface = ipaddress.ip_interface(data.strip())
|
||||||
network_string = str(interface.network).split('/')[0]
|
network_string = str(interface.network).split('/')[0]
|
||||||
network_cidr = 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}')
|
||||||
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 = next(network.hosts())
|
first_host = str(next(network.hosts()))
|
||||||
|
|
||||||
# hack to speed up iterating through large ipv6 subnets
|
# hack to speed up iterating through large ipv6 subnets
|
||||||
# only do last_host for masks >= /16 (ipv4) or >= /120 (ipv6)
|
# only do last_host for masks >= /16 (ipv4) or >= /120 (ipv6)
|
||||||
last_host = None
|
last_host = None
|
||||||
|
|
||||||
if any((
|
if any((
|
||||||
int(interface.version) == 4 and int(network_cidr) >= 16,
|
int(interface.version) == 4 and network_cidr >= 16,
|
||||||
int(interface.version) == 6 and int(network_cidr) >= 120,
|
int(interface.version) == 6 and network_cidr >= 120,
|
||||||
)):
|
)):
|
||||||
dd = deque(network.hosts(), maxlen=1)
|
dd = deque(network.hosts(), maxlen=1)
|
||||||
last_host = str(dd.pop())
|
last_host = str(dd.pop())
|
||||||
@ -130,8 +130,8 @@ def parse(
|
|||||||
'broadcast': str(network.broadcast_address),
|
'broadcast': str(network.broadcast_address),
|
||||||
'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': int(network_cidr),
|
'cidr_netmask': network_cidr,
|
||||||
'first_host': str(first_host),
|
'first_host': first_host,
|
||||||
'last_host': last_host, # None if netmask is too small
|
'last_host': last_host, # None if netmask is too small
|
||||||
'is_multicast': interface.is_multicast,
|
'is_multicast': interface.is_multicast,
|
||||||
'is_private': interface.is_private,
|
'is_private': interface.is_private,
|
||||||
|
Reference in New Issue
Block a user