python-3.x - Netmiko/Python 脚本不显示来自多个设备的输出
问题描述
当我运行脚本时,它只返回第一个设备的输出。
#!/usr/local/bin/python3.6
import netmiko
from netmiko import ConnectHandler
import getpass
from getpass import getpass
exceptions = (netmiko.ssh_exception.NetMikoTimeoutException, netmiko.ssh_exception.NetMikoAuthenticationException)
router = {
'device_type': 'cisco_ios',
'ip': '10.5.5.1',
'username': 'admin',
'password': getpass(),
'secret': getpass("Enable: "),
'global_delay_factor': 2,
}
switch = {
'device_type': 'cisco_ios',
'ip': '10.5.5.2',
'username': 'admin',
'password': getpass(),
'secret': getpass("Enable: "),
'global_delay_factor': 2,
}
list_of_devices = [router, switch]
for devices in list_of_devices:
connector = ConnectHandler(**devices)
connector.enable()
print(connector)
output = connector.find_prompt()
output += connector.send_command('show ip arp', delay_factor=2)
print(output)
connector.disconnect()
解决方案
您需要在 for 循环中包含所有 Netmiko 操作。使用您当前的代码,您在第一台设备上建立连接,然后转到第二台设备并对其进行操作。您实际上并没有对第一个设备做任何事情(因为 for 循环内唯一的事情是 ConnectHandler 调用):
所以像这样(for循环部分):
list_of_devices = [router, switch]
for devices in list_of_devices:
connector = ConnectHandler(**devices)
connector.enable()
print(connector)
output = connector.find_prompt()
output += connector.send_command('show ip arp', delay_factor=2)
print(output)
connector.disconnect()
推荐阅读
- expression - 在 SymPy 中,我如何构建一个改变输入表达式的函数?
- python - 将数据框中的日期时间保存为 csv 时保持时区
- azure-active-directory - Ms Teams OnlineMeeting 匿名用户加入认证
- java - 将 XML 文档转换为 ArrayList 时的 NPE
- docker - Windows 容器的代理设置
- api - 使用 API 将数据拉入 PowerBI
- vue.js - Vercel 和 Nuxt 部署上的 FUNCTION_INVOCATION_FAILED 错误
- java - 将反序列化的 java 对象转换为 JSON
- java - 如果套件在 BeforeSuite 中失败,则显示失败
- visual-studio-code - 搜索功能是否需要输入键才能执行?