我有下面的代码,它将打开包含 IP 地址的 .txt 文件,然后连接到设备并捕获命令输出,然后它将输出打印到文件并声明一切正常。
我无法让它遍历一系列 IP 地址并返回多个设备的命令输出。当我将多个 IP 添加到 .txt 列表时,我收到脚本超时错误。这可以通过两次添加相同的地址来证明,因此我知道这些地址是好的,而文件中只有一个地址并且它可以无缝地工作。
我正在寻找一种方法来遍历 10 个 IP 地址并运行相同的命令:
from __future__ import print_function
from netmiko import ConnectHandler
import sys
import time
import select
import paramiko
import re
fd = open(r'C:\Users\NewdayTest.txt','w')
old_stdout = sys.stdout
sys.stdout = fd
platform = 'cisco_ios'
username = 'Username'
password = 'Password'
ip_add_file = open(r'C:\Users\\IPAddressList.txt','r')
for host in ip_add_file:
device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
output = device.send_command('terminal length 0')
output = device.send_command('enable')
print('##############################################################\n')
print('...................CISCO COMMAND SHOW RUN OUTPUT......................\n')
output = device.send_command('sh run')
print(output)
print('##############################################################\n')
print('...................CISCO COMMAND SHOW IP INT BR OUTPUT......................\n')
output = device.send_command('sh ip int br')
print(output)
print('##############################################################\n')
fd.close()