python3 telnet read_all()不起作用

时间:2017-11-03 01:35:02

标签: python bash telnet cisco telnetlib

我通过Python3 telnet到Cisco路由器。但是,它在运行脚本后挂起(但我可以从我的Linux bash telnet到路由器)。请参阅下面的脚本和输出的片段。

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

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

4 个答案:

答案 0 :(得分:3)

telnetlib中的

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

答案 1 :(得分:1)

Telnet.read_all():读取所有数据,直到EOF为止;阻塞直到连接关闭。 因此,您必须使用 exit 命令指示它是文件结束。 您的代码应该像

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'))

答案 2 :(得分:1)

您需要先发送命令“ terminal length 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'))

答案 3 :(得分:0)

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

请确保在路由器中配置了:

  • aaa新型
  • aaa身份验证登录默认本地
  • line 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())