python3 telnet read_all()不起作用

Posted

技术标签:

【中文标题】python3 telnet read_all()不起作用【英文标题】:python3 telnet read_all() doesn't work 【发布时间】:2018-04-15 16:13:32 【问题描述】:

我正在通过 Python3 远程登录到 Cisco 路由器。但是,它在运行脚本后挂断了(但我可以从我的 Linux bash 远程登录到路由器)。请在下面查看我的脚本和输出的 sn-ps。

import getpass
import telnetlib
HOST = "10.10.32.3"
user = input("Enter your telnet username: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")
tn.write(b"conf t\n")
tn.write(b"int l0\n")
print(tn.read_all().decode('ascii'))

这是debug telnet在路由器上的输出

Router#
*Nov 2 23:48:24.317: Telnet578: 1 1 251 1
*Nov 2 23:48:24.318: TCP578: Telnet sent WILL ECHO (1)
*Nov 2 23:48:24.318: Telnet578: 2 2 251 3
*Nov 2 23:48:24.318: TCP578: Telnet sent WILL SUPPRESS-GA (3)
*Nov 2 23:48:24.318: Telnet578: 80000 80000 253 24
*Nov 2 23:48:24.319: TCP578: Telnet sent DO TTY-TYPE (24)
*Nov 2 23:48:24.319: Telnet578: 10000000 10000000 253 31
*Nov 2 23:48:24.319: TCP578: Telnet sent DO WINDOW-SIZE (31)
*Nov 2 23:48:24.383: TCP578: Telnet received DONT ECHO (1)
*Nov 2 23:48:24.383: TCP578: Telnet sent WONT ECHO (1)
*Nov 2 23:48:24.387: TCP578: Telnet received DONT SUPPRESS-GA (3)
*Nov 2 23:48:24.387: TCP578: Telnet sent WONT SUPPRESS-GA (3)
Router#
*Nov 2 23:48:24.389: TCP578: Telnet received WONT TTY-TYPE (24)
*Nov 2 23:48:24.389: TCP578: Telnet sent DONT TTY-TYPE (24)
*Nov 2 23:48:24.390: TCP578: Telnet received WONT WINDOW-SIZE (31)
*Nov 2 23:48:24.391: TCP578: Telnet sent DONT WINDOW-SIZE (31)
*Nov 2 23:48:24.407: TCP578: Telnet received DONT ECHO (1)
*Nov 2 23:48:24.407: TCP578: Telnet received DONT SUPPRESS-GA (3)
*Nov 2 23:48:24.407: TCP578: Telnet received WONT TTY-TYPE (24)
*Nov 2 23:48:24.408: TCP578: Telnet received WONT WINDOW-SIZE (31)

还有show tcp brief的输出

Router#sho tcp brief 
TCB       Local Address               Foreign Address             (state)
10C90CE0  10.10.32.3.23              192.168.122.61.51466        ESTAB

我可以创建环回接口,但我的 Linux bash 没有显示 telnet 输出。请相应指导。谢谢。

【问题讨论】:

【参考方案1】:

.read_all() 在 telnetlib 中记录为“读取所有数据直到 EOF;阻塞直到连接关闭。”。由于您没有做任何会导致连接关闭的事情,因此挂起正是您应该期望在这里发生的事情。尝试先向路由器发送退出命令。或者,如果您打算根据读取的结果发出更多命令,请改用.read_until()(可能指定超时)。

【讨论】:

【参考方案2】:

Telnet.read_all() : 读取所有数据直到 EOF;阻塞直到连接关闭。 因此,您必须使用 exit 命令指明它是 End Of File。 你的代码应该是这样的

import getpass
import telnetlib
HOST = "10.10.32.3"
user = input("Enter your telnet username: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")
tn.write(b"conf t\n")
tn.write(b"int l0\n")
tn.write("end\n")
tn.write("exit\n")
print(tn.read_all().decode('ascii'))

【讨论】:

我在给出退出或注销命令之前关闭了 telnet 连接,现在我终于明白 read_all 了..【参考方案3】:

您需要在其他人之前发送命令“终端长度 0”。这是我获取配置的脚本:

import getpass
import telnetlib

HOST = "192.168.0.X"
user = input("Enter your username: ")
password = getpass.getpass()

#Iniciando sesion telnet
tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")

tn.write(b"terminal length 0\n")
tn.write(b"show runn\n")
tn.write(b"exit\n")

print(tn.read_all().decode('ascii'))

【讨论】:

【参考方案4】:

您的代码是正确的,但应该像这样更改一些行,重要的是最后一行:

确保在您的路由器中配置:

aaa 新型号 aaa认证登录默认本地 线 vty 0 15 传输输入全部 默认登录验证
import getpass
import telnetlib

HOST = "10.10.32.3"
user = input("Enter your telnet username: ")

password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")
# Be sure that if you configured enable password you should use this section :
tn.write(b"enable\n")
tn.write(b"cisco\n")
# If you did not use enable password start from this section
tn.write(b"conf t\n")
tn.write(b"int l0\n")
# sample config that you would be sure that you changed your configuration
tn.write(b"des THIS IS JUST FOR TEST\n")
tn.write(b"end\n")
tn.write(b"exit\n")
# **End section should be exactly like this line**
print(tn.read_all())

【讨论】:

以上是关于python3 telnet read_all()不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Python3+telnetlib实现telnet客户端

python3+telnetlib实现简单自动测试

Python Telnet 连接

python3巡检vcenter6.7快照,发带表格邮件

用Python怎么telnet到网络设备

Telnet客户端没有收到。 Python telnetlib作为服务器