Created
January 7, 2022 15:39
-
-
Save Torxed/148303f1677e56dcca794386e90299fc to your computer and use it in GitHub Desktop.
Converts a CIDR notation to netmask
This file contains 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 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