Python 在IP地址列表中循环
我有下面的代码,它将在.txt文件中查找IP地址,然后转到设备并返回命令,然后打印到请求的文件中,一切正常 我不能让它做的是循环通过一系列IP地址并返回不同设备的命令。当我将多个IP添加到.txt列表中时,脚本出现超时错误,通过两次添加相同的地址证明了这一点,因此我知道这些地址是好的,因为当文件中只有一个地址时,它工作得很顺利 我正在寻找一种方法,在10个IP地址之间循环,并在所有这些都说了和做了之后运行相同的命令Python 在IP地址列表中循环,python,python-3.x,cisco,Python,Python 3.x,Cisco,我有下面的代码,它将在.txt文件中查找IP地址,然后转到设备并返回命令,然后打印到请求的文件中,一切正常 我不能让它做的是循环通过一系列IP地址并返回不同设备的命令。当我将多个IP添加到.txt列表中时,脚本出现超时错误,通过两次添加相同的地址证明了这一点,因此我知道这些地址是好的,因为当文件中只有一个地址时,它工作得很顺利 我正在寻找一种方法,在10个IP地址之间循环,并在所有这些都说了和做了之后运行相同的命令 from __future__ import print_function fr
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()
为了清楚起见,下面给出的答案非常适合我的要求,因此代码的开头如下所示:
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:
host = host.strip()
device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
output = device.send_command('terminal length 0')
我猜您的
ConnectHandler
不知道在主机
字符串的末尾有一个换行符
试试这个:
for host in ip_add_file:
host = host.strip()
...
我猜您的
ConnectHandler
不知道在host
字符串的末尾有一个换行符
试试这个:
for host in ip_add_file:
host = host.strip()
...
你能提供一个例子来说明你的IPAddressList.txt文件是什么样子的吗?IP地址只是以纯文本的形式保存在每一行上,如下所示:当我保存它时,它显示在同一行上,但它们都在自己的行上。10.22.139.6 10.22.139.1 10.22.139.4 10.22.139.33 10.22.139.88 10.22.139.5能否提供一个示例,显示您的IPAddressList.txt文件的外观?IP地址只是以纯文本形式保存在每一行的新行上,如下所示:当我保存时,它显示在同一行上,但它们都在自己的行上。10.22.139.6 10.22.139.1 10.22.139.4 10.22.139.33 10.22.139.88 10.22.139.5你是一颗绝对的明星,这是完美的,当你得到答案时,你总是显得如此明显。谢谢你,真的谢谢你,你是一个绝对的明星,这是完美的,就像你得到答案时总是那么明显。谢谢你真的谢谢你