Last active
August 29, 2015 14:28
-
-
Save vcabbage/4eb22a1d598d3154c8cb to your computer and use it in GitHub Desktop.
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
from ipaddress import ip_address | |
def main(): | |
print(get_mask_length('192.168.5.65', '192.168.5.111')) | |
print(get_mask_length('2001:dead::', '2001:dead:beef::')) | |
def get_mask_length(ip1, ip2): | |
ip1_int = int(ip_address(ip1)) | |
ip2_int = int(ip_address(ip2)) | |
unique_bits_int = abs(ip1_int - ip2_int) + 1 #add 1 to be inclusive of both addresses | |
return ip_address(ip1).max_prefixlen - len('{:b}'.format(unique_bits_int)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment