我用python编写了一个小代码,用telnet库在多个Cisco路由器上自动执行配置任务。在第一次迭代中一切正常,问题是我在函数read_all()的第二次迭代中超时。如果我从telnetlib.远程登录()函数,我永远被困在同一个迭代中。

在from telnet import telnet
import xlrd
class handler:
excel_sheet=None
ipv4_devices=None
ipv6_devices=None
config=None
telnet=None
def __init__(self):
self.excel_sheet = xlrd.open_workbook("ipv4_devices.xlsx")
self.config=open("config.txt","r")
output_init = open("output.txt","w+")
output_init.write("")
output_init.close()
def execute(self):
row = None
column = None
self.ipv4_devices = self.excel_sheet.sheet_by_index(0)
for row in range(self.ipv4_devices.nrows-1):
self.telnet = telnet(self.ipv4_devices.cell_value(row+1,0),self.ipv4_devices.cell_value(row+1,1), self.ipv4_devices.cell_value(row+1,2), self.ipv4_devices.cell_value(row+1,3), self.config)
self.telnet.do_telnet()
self.telnet=None
self.config.close()

上面是我的处理程序.py文件,它反过来调用我的telnet.py网站包含实际telnet代码的文件。

在^{pr2}$下面是我超时时得到的错误

Traceback (most recent call last):
File "Automate.py", line 5, in 
automate_it.execute()
File "/home/abdultayyeb/Desktop/Automation/handler.py", line 30, in execute
self.telnet.do_telnet()
File "/home/abdultayyeb/Desktop/Automation/telnet.py", line 31, in do_telnet
output.write(remote.read_all()+"\n"+" ------- "+"\n")
File "/usr/lib/python2.7/telnetlib.py", line 385, in read_all
self.fill_rawq()
File "/usr/lib/python2.7/telnetlib.py", line 576, in fill_rawq
buf = self.sock.recv(50)
socket.timeout: timed out