Skip to content

Instantly share code, notes, and snippets.

@Torxed
Created January 7, 2022 15:39
Show Gist options
  • Save Torxed/148303f1677e56dcca794386e90299fc to your computer and use it in GitHub Desktop.
Save Torxed/148303f1677e56dcca794386e90299fc to your computer and use it in GitHub Desktop.
Converts a CIDR notation to netmask
def cidr_to_netmask(cidr :int, bitlen=32) -> str:
netmask = ''
while cidr > 8:
netmask += '255.'
cidr -= 8
netmask += str(2 ** cidr - 1) # math.log(128, 2)
return netmask + ('.0' * (3 - netmask.count('.') if bitlen == 32 else 7 - netmask.count('.')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment