Skip to content

Instantly share code, notes, and snippets.

@thepacketgeek
Created July 24, 2015 15:19
Show Gist options
  • Save thepacketgeek/1fe3b4522dccfe377599 to your computer and use it in GitHub Desktop.
Save thepacketgeek/1fe3b4522dccfe377599 to your computer and use it in GitHub Desktop.
Convert Subnet Mask to CIDR length
def calc_mask_length(mask):
""" Convert subnet mask (255.255.255.0) to CIDR mask length (/24) """
return sum([bin(int(x)).count('1') for x in mask.split('.')])
print calc_mask_length('255.255.255.0')
# 24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment