Created
October 6, 2024 08:39
-
-
Save mjrider/e8215d8e9e27008cc2c4cd6e729a12a9 to your computer and use it in GitHub Desktop.
ripe-ip-adjuster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def adjust_to_network(ip_in, end_ip_in): | |
# Convert starting IP to its network address and ensure it's aligned to the nearest network boundary | |
start_ip = ipaddress.IPv4Address(int(ipaddress.IPv4Network(ip_in, strict=False).network_address) & ~1) | |
# Convert ending IP to its network address | |
end_ip = ipaddress.IPv4Network(end_ip_in, strict=False).network_address | |
end_ip_int = int(end_ip) | |
# Adjust the end IP based on the last 2 bits, ensuring the last bit is always 1 | |
mask = end_ip_int % 4 | |
if mask == 0b00: | |
end_ip_int -= 1 # Move to the previous valid broadcast address | |
elif mask == 0b10: | |
end_ip_int += 1 # Move to the next valid address | |
end_ip = ipaddress.IPv4Address(end_ip_int) | |
return start_ip, end_ip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment