Last active
May 4, 2017 12:19
-
-
Save Gasoid/eaea70b8dcdd5d290aa5d4b708b03b60 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
def net_mask(cidr): | |
cidr, mask = cidr.split('/') | |
mask = int(mask) | |
bin_mask_str = mask * '1' + (32 - mask) * '0' | |
mask_lst = [int(bin_mask_str[n*8:(n+1)*8], 2) for n in range(4)] | |
net_addr = [] | |
broadcast = [] | |
for oct in zip(cidr.split('.'), mask_lst): | |
net_addr.append(int(oct[0]) & oct[1]) | |
broadcast.append(int(oct[0]) | (255 - oct[1])) | |
return '.'.join(map(str, net_addr)), '.'.join(map(str, broadcast)) | |
# example | |
net_mask('192.168.23.16/21') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment