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

我是蟒蛇的新手。我有使用 netmiko 在多个交换机上运行多个“显示命令”的代码,当一切都在循环中时它工作正常。但是当我想通过将其分配为变量并打印它来将多个“显示命令”的输出带到循环外时,只打印输出之一。


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)


互换的青春
浏览 112回答 1
1回答

喵喔喔

尝试这个: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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python