Generate hosts and switches, with IP addressing

 avatar
unknown
python
2 years ago
2.3 kB
5
Indexable
# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
def generate_topology(x=4, y=2, z=3):
    spine_layer_switches = []
    leaf_layer_switches = []
    hosts = []
    
    spine_layer_ip_subnets = []
    leaf_layer_ip_subnets = []
    host_ip_subnets = []
    
    # Mapping of spine layer ip against switch name
    spine_layer_ip_subnet_x_switch = {}
    leaf_layer_ip_subnet_x_switch = {}
    ip_subnet_x_host = {}
    host_x_ip = {}
    # Note that 'x' denotes 'mapping of'
    
    first_ip_subnet = 10
    for i in range(0, x):
        layer_1_subnet = str(first_ip_subnet + i)
        spine_layer_switches.append("s1_{}".format(i))
        spine_layer_ip_subnets.append(layer_1_subnet)
        spine_layer_ip_subnet_x_switch[layer_1_subnet] = spine_layer_switches[-1]
        
        # For every spine layer switch, I'm adding 'y' leaf layer switches
        for j in range(0, y):
            layer_2_subnet = layer_1_subnet + ".{}".format(j)
            leaf_layer_switches.append("s2_{}".format(y*i + j))
            leaf_layer_ip_subnets.append(layer_2_subnet) 
            leaf_layer_ip_subnet_x_switch[layer_2_subnet] = leaf_layer_switches[-1]
            
            # For every leaf layer switch, I'm adding 'z' leaf switches
            for k in range(0, z):
                host_layer_subnet = layer_2_subnet + ".{}".format(k)
                hosts.append("h{}".format(z*y*i + z*j + k))
                host_ip_subnets.append(host_layer_subnet)
                ip_subnet_x_host[host_layer_subnet] = hosts[-1]
                host_x_ip[hosts[-1]] = host_layer_subnet + ".0"
        
    print(spine_layer_switches)
    print(leaf_layer_switches)
    print(hosts)
    
    print("\n----------------------------------------\n")
    print(spine_layer_ip_subnet_x_switch)
    print(leaf_layer_ip_subnet_x_switch)
    print(host_ip_subnets)
    
    
    print("\n----------------------------------------\n")
    print(host_x_ip)
    
    print("\n----------------------------------------\n")
    for i in ip_subnet_x_host.items():
        print(i)
        
    print("\n----------------------------------------\n")
    for i in host_x_ip.items():
        print(i)
    
    
    
generate_topology()
Editor is loading...