Skip to content

Instantly share code, notes, and snippets.

@vcabbage
Last active August 29, 2015 14:28
Show Gist options
  • Save vcabbage/4eb22a1d598d3154c8cb to your computer and use it in GitHub Desktop.
Save vcabbage/4eb22a1d598d3154c8cb to your computer and use it in GitHub Desktop.
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