SHARE
TWEET

Untitled

a guest Jul 24th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def valid_subnet_masks():
  2.     '''Generates a list of tuples containing the CIDR notation, dotted-decimal notation, and binary string representation of
  3.     subnet masks /1 - /32
  4.  
  5.     :return: All possible ipv4 subnet masks in each of there major formats CIDR, dotted-decimal, and binary
  6.     '''
  7.     valid_masks = []
  8.  
  9.     # Create list of 32 binary strings of cidr notations from 1 - 32
  10.     cidrs = [[x, '1' * x + '0' * (32 - x)] for x in range(1, 33)]
  11.  
  12.     for cidr, binary_string in enumerate(cidrs, start=1):
  13.         binary_string = binary_string[1]
  14.         # slice binary string into four 8 bit chunks
  15.         octet_1 = binary_string[:8]
  16.         octet_2 = binary_string[8:16]
  17.         octet_3 = binary_string[16:24]
  18.         octet_4 = binary_string[24:32]
  19.         # convert each 8 bit string into an integer and add that to our list of subnets in its corresponding index
  20.         valid_masks.append((cidr,
  21.                             '{}.{}.{}.{}'.format(int(octet_1, 2), int(octet_2, 2), int(octet_3, 2), int(octet_4, 2)),
  22.                             binary_string))
  23.     return valid_masks
  24.  
  25.  
  26. if __name__ == '__main__':
  27.    
  28.     for x in valid_subnet_masks():
  29.         print(x)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top