簡體   English   中英

python3 telnet read_all() 不起作用

[英]python3 telnet read_all() doesn't work

我正在通過 Python3 遠程登錄到 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

我可以創建環回接口,但我的 Linux bash 沒有顯示 telnet 輸出。 請相應指導。 謝謝。

.read_all()被記錄為“讀取所有數據直到 EOF;阻塞直到連接關閉。”。 由於您沒有做任何會導致連接關閉的事情,因此掛起正是您應該在這里發生的事情。 首先嘗試向路由器發送退出命令。 或者,如果您打算根據讀取的結果發出更多命令,請改用.read_until() (可能指定超時)。

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

您需要在其他人之前發送命令“終端長度 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'))

你的代碼是正確的,但應該改變一些這樣的行,重要的是最后一行:

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

  • 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())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM