IPv4 prefixes are added to IPv6
Opened this issue · 2 comments
arianniaki commented
need to add statement to check if IP is IPv6 in order not to add the /24 prefix to it.
zackw commented
Python 3 has a module ipaddress
for validating and working with IP address and network literals. Code to do what we want properly would be something like this:
def mask_host_address(addr_str):
addr = ipaddress.ip_address(addr_str)
if addr.version == 4:
masked = ipaddress.IPv4Network((addr, 24), strict=False)
elif addr.version == 6:
masked = ipaddress.IPv6Network((addr, 64), strict=False)
else:
raise ValueError("don't know proper mask length for '{}' (IPv{})"
.format(addr_str, addr.version))
return str(masked)
This would replace the existing code to tack .0/24
on the end.