Open arianniaki opened 5 years ago
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.
need to add statement to check if IP is IPv6 in order not to add the /24 prefix to it.