如何在python(netmiko)中从循环获取多个输出到外部

2024-04-24 09:19:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的新手。我有使用netmiko在多个交换机上运行多个“show命令”的代码,当所有东西都在循环中时,它工作得很好。但是,当我想通过将多个“show commands”的输出赋值为变量并打印它,从而将其输出到循环之外时,只打印其中一个输出

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)

Tags: ipnetadmindevicetypeshowconnectusername
1条回答
网友
1楼 · 发布于 2024-04-24 09:19:45

试试这个:

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)

相关问题 更多 >