运行脚本时,它仅返回第一台设备的输出。
#!/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()
答案 0 :(得分:0)
您需要在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()