卡在 Python 中的 read_all() 函数?
Stuck with read_all() function in Python?
我在 python 中编写了一小段代码,使用 telnet 库在多个 Cisco 路由器上自动执行配置任务。在第一次迭代中一切正常,问题是我在函数 read_all() 的第二次迭代中超时。如果我从 telnetlib.Telnet() 函数中删除超时参数,我将永远陷入同一次迭代。
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()
以上是我的 Handler.py 文件,它又调用我的 telnet.py 文件,其中包含实际的 telnet 代码。
import sys
import telnetlib
class telnet:
ipv4_address=None
port=0
username=None
passphrase=None
config=None
def __init__(self, ipv4_address=None, port=23, username=None, passphrase=None, config=None):
if (ipv4_address is not None) and (username is not None) and (passphrase is not None) and (config is not None) :
self.ipv4_address = ipv4_address
self.port = port
self.username = username
self.passphrase = passphrase
self.config = config
else:
exit()
def do_telnet(self):
output = open("output.txt","a")
remote= telnetlib.Telnet(self.ipv4_address,int(self.port))
remote.read_until("Username: ")
remote.write(self.username.encode('ascii')+"\n")
remote.read_until("Password: ")
remote.write(self.passphrase.encode('ascii')+"\n")
remote.write(self.config.read().encode('ascii')+"\n")
output.write(remote.read_all()+"\n"+" ------- "+"\n")
remote.close()
output.close()
下面是我超时时得到的错误
Traceback (most recent call last):
File "Automate.py", line 5, in <module>
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
我建议重新编写此代码以使用 netmiko,因为它将处理与 Cisco 打交道的特殊情况 IOS。
关键是将您的设备类型指定为cisco_ios_telnet
。
这将带来额外的好处,即在将系统转换为 SSH 时无需重新编写代码。您只需将设备类型从 cisco_ios_telnet
更改为 cisco_ios
我在 python 中编写了一小段代码,使用 telnet 库在多个 Cisco 路由器上自动执行配置任务。在第一次迭代中一切正常,问题是我在函数 read_all() 的第二次迭代中超时。如果我从 telnetlib.Telnet() 函数中删除超时参数,我将永远陷入同一次迭代。
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()
以上是我的 Handler.py 文件,它又调用我的 telnet.py 文件,其中包含实际的 telnet 代码。
import sys
import telnetlib
class telnet:
ipv4_address=None
port=0
username=None
passphrase=None
config=None
def __init__(self, ipv4_address=None, port=23, username=None, passphrase=None, config=None):
if (ipv4_address is not None) and (username is not None) and (passphrase is not None) and (config is not None) :
self.ipv4_address = ipv4_address
self.port = port
self.username = username
self.passphrase = passphrase
self.config = config
else:
exit()
def do_telnet(self):
output = open("output.txt","a")
remote= telnetlib.Telnet(self.ipv4_address,int(self.port))
remote.read_until("Username: ")
remote.write(self.username.encode('ascii')+"\n")
remote.read_until("Password: ")
remote.write(self.passphrase.encode('ascii')+"\n")
remote.write(self.config.read().encode('ascii')+"\n")
output.write(remote.read_all()+"\n"+" ------- "+"\n")
remote.close()
output.close()
下面是我超时时得到的错误
Traceback (most recent call last):
File "Automate.py", line 5, in <module>
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
我建议重新编写此代码以使用 netmiko,因为它将处理与 Cisco 打交道的特殊情况 IOS。
关键是将您的设备类型指定为cisco_ios_telnet
。
这将带来额外的好处,即在将系统转换为 SSH 时无需重新编写代码。您只需将设备类型从 cisco_ios_telnet
更改为 cisco_ios