sandbox/network/ipv4_subnet_calc.py

19 lines
575 B
Python

# 255.255.255.0 24
# 255.255.0.0 16
# 255.0.0.0 8
def subnet_mask_to_cidr(subnet_mask):
# Split the subnet mask into its octets
octets = subnet_mask.split('.')
# Convert each octet to binary and count the number of 1s
binary_representation = ''.join(format(int(octet), '08b') for octet in octets)
print(binary_representation)
cidr = binary_representation.count('1')
return cidr
# Example usage
subnet_mask = '255.255.255.240'
cidr = subnet_mask_to_cidr(subnet_mask)
print(f"The CIDR notation for subnet mask {subnet_mask} is /{cidr}")