Search code examples
pythonpython-3.xnetmiko

how to take multiple outputs from a loop to outside in python(netmiko)


I am a newbie to the python. I have code to run multiple 'show commands' on multiple switches using netmiko, which is working fine when everything is within the loop. But when I want to take this output of the multiple 'show commands' outside the loop by assigning it as a variable and print it, only prints one of the outputs.

S1 = {
    'device_type': 'cisco_ios',
    'ip': '192.168.0.56',
    'username': 'admin',
    'password': 'admin'
    }

S2= {
    'device_type': 'cisco_ios',
    'ip': '192.168.0.57',
    'username': 'admin',
    'password': 'admin'
    }

all_devices = [S1,S2]


for devices in all_devices:
    print("\nLogging into the switch...")
    net_connect = ConnectHandler(**devices)
    net_connect.enable()
    cmd = ["show vlan brief", "\n","\n","show ip interface brief"]
    for show in cmd:
        output=net_connect.send_command(show)
        y = output

print(y)

Solution

  • Try this:

    S1 = {
        'device_type': 'cisco_ios',
        'ip': '192.168.0.56',
        'username': 'admin',
        'password': 'admin'
        }
    
    S2= {
        'device_type': 'cisco_ios',
        'ip': '192.168.0.57',
        'username': 'admin',
        'password': 'admin'
        }
    
    all_devices = [S1,S2]
    
    y = []
    
    for devices in all_devices:
        print("\nLogging into the switch...")
        net_connect = ConnectHandler(**devices)
        net_connect.enable()
        cmd = ["show vlan brief", "\n","\n","show ip interface brief"]
        for show in cmd:
            output=net_connect.send_command(show)
            y.append(output)
    
    for x in y:
        print(x)