Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.1 kB
1
Indexable
Never
def list_smaller_cidr_in_bigger_cidr(bigger_cidr):
    bigger_network, bigger_prefix = bigger_cidr.split('/')
    bigger_prefix = int(bigger_prefix)
    bigger_network_octets = [int(octet) for octet in bigger_network.split('.')]
    
    smaller_cidrs = []
    
    # Iterate through all possible prefix lengths for smaller CIDR ranges
    for prefix_length in range(16, 25):  # From /16 to /24
        # Calculate the number of smaller CIDR ranges
        num_smaller_cidr = 2 ** (prefix_length - bigger_prefix)
        
        # Iterate through all possible smaller CIDR ranges
        for i in range(num_smaller_cidr):
            # Calculate the new network address for the smaller CIDR
            new_network = '.'.join(str(bigger_network_octets[j] + (i >> (24 - (j * 8))) & 255) for j in range(4))
            smaller_cidr = f"{new_network}/{prefix_length}"
            smaller_cidrs.append(smaller_cidr)
    
    return smaller_cidrs

# Example usage
bigger_cidr = '192.168.0.0/16'
smaller_cidrs = list_smaller_cidr_in_bigger_cidr(bigger_cidr)
print(smaller_cidrs)
Leave a Comment