3

我正在尝试使用 telnetlib 从 cisco 路由器读取

import telnetlib
tn = telnetlib.Telnet(’10.106.218.50’, 17280)
cmd1=”enable”
cmd2=”show run”
#session.write("command".encode('ascii') + b"\r")
tn.write(cmd1.encode('ascii') + b"\r")
tn.write(cmd2.encode('ascii') + b"\r")
#op=tn.read_very_eager()
#op=tn.read_some()
#op=tn.read_until('#')
op=tn.read_all()
print op

我能够成功写入路由器的控制台但是系统只是挂起,当我尝试从路由器的控制台读取时。 当我使用 read_some() 时,我得到了输出的一部分。但是 read_all() 只是挂起并且没有响应请提出解决方案

4

3 回答 3

3

read_all()

如果在建立连接时没有指定超时,python 的 telnetlib 模块中的命令将阻塞。

您的调用命令应如下所示

tn = telnetlib.Telnet('10.106.218.50', 17280, timeout = 1)

您也可以替换您自己的超时值。

于 2015-05-06T18:44:22.723 回答
3

我的解决方案是使用 tn.red_until()。这段代码对我有用。如果您不需要将输出发送到文本文件,请修改:

data = data + tn.read_until(b"FIN\n", timeout = TIMEOUT).decode('ascii')

经过:

tn.read_until(b"FIN\n", timeout = TIMEOUT)

这是我的代码:

import sys
import telnetlib
import time

username = "admin"
password = "admin"
command = "show version"
TIMEOUT = 3

router_list = open("hostlist.txt")
for line in router_list:
    tn = telnetlib.Telnet(line.rstrip())
    tn.set_debuglevel(1)
    time.sleep(2)
    data = data + tn.read_until(b"Username: ").decode('ascii')
    tn.write(username.encode('ascii') + b"\n")
    time.sleep(2)
    data = data + tn.read_until(b"Password: ").decode('ascii')
    tn.write(password.encode('ascii') + b"\n")
    time.sleep(2)
    tn.write(command.encode('ascii') + b"\n")
    time.sleep(2)
    tn.write(b"\n")
    time.sleep(2)
    tn.write(b"echo FIN\n")
    time.sleep(2)
    data = data + tn.read_until(b"FIN\n", timeout = TIMEOUT).decode('ascii')
    print("Imprimiendo:" + data)
    op=open ("output.txt", "a").write(data)
    tn.close()
于 2020-08-11T14:32:40.117 回答
0

我遇到了同样的问题。

import socket, telnetlib

def telnet(ip_address,user_name,password):

    tn = telnetlib.Telnet(ip_address,23,1)
    # tn.set_debuglevel(2)

    tn.write(user_name + '\n')
    tn.write(password + '\n')
    tn.write('show version\n')
    tn.write('show power\n')

    print tn.read_all()
    tn.close()

telnet('10.236.0.19','who','who')

错误日志在这里是一样的:

Traceback (most recent call last):
  File "telnet.py", line 41, in <module>
    telnet('10.236.0.19','who','who')
  File "telnet_tn.py", line 23, in telnet
    print tn.read_all()
  File "C:\Python27\lib\telnetlib.py", line 385, in read_all
    self.fill_rawq()
  File "C:\Python27\lib\telnetlib.py", line 576, in fill_rawq
    buf = self.sock.recv(50)
socket.timeout: timed out

然后我尝试修改C:\Python27\lib\telnetlib.py中的函数read_all()。之后,它按我的预期工作。你可以试试。。

前:

def read_all(self):
    """Read all data until EOF; block until connection closed."""
    self.process_rawq()
    while not self.eof:
        self.fill_rawq()
        self.process_rawq()
    buf = self.cookedq
    self.cookedq = ''
    return buf

之后(为socket.timeout添加一个例外):

def read_all(self):
    """Read all data until EOF; block until connection closed."""
    self.process_rawq()
    while not self.eof:
        try:
            self.fill_rawq()
            self.process_rawq()
        except:
            break
    buf = self.cookedq
    self.cookedq = ''
    return buf
于 2019-08-17T10:04:29.400 回答