Python Telnet 连接 [英] Python Telnet connection
问题描述
当我遇到问题时,我正在尝试使用 python 3.1.我想连接到 telnet 服务器.这是我的代码:
I was playing around with python 3.1 when I came to a problem. I want to connect to a telnet server. Here is my code:
import sys
import telnetlib
tn = telnetlib.Telnet("10.0.0.138")
tn.read_until(b"Username :", 2)
tn.write(b"\n")
tn.read_until(b"Password :", 2)
tn.write(b"\n")
tn.read_until(b"=>", 2)
tn.write(b"exit\n")
tn.close
它可以一直读到用户名:".写入空行时也没有错误消息.但是当我读到密码:"时,我得到一个空字符串.当我阅读所有内容时,我也得到一个空字符串.
It works to read until "Username :". There is also no error message when writing an emty line. But when i read until "Password :" i get an empty string. I also get an empty string when i read all.
如果可以,请帮助我.
这是我通过 putty 连接到服务器时的输出.
Here is the output when i connect to the server via putty.
Willkommen am THOMSON TG787v
Plattform:VDNT-D Firmware:8.2.5.0 Seriennummer:CP0919MT238
Bitte identifizieren Sie sich mit Ihrem Benutzernamen und Kennwort
--------------------------------------------------------------------------------
Username :
Password :
------------------------------------------------------------------------
______ Thomson TG787v
___/_____/\
/ /\\ 8.2.5.0
_____/__ / \\
_/ /\_____/___ \ Copyright (c) 1999-2009, THOMSON
// / \ /\ \
_______//_______/ \ / _\/______
/ / \ \ / / / /\
__/ / \ \ / / / / _\__
/ / / \_______\/ / / / / /\
/_/______/___________________/ /________/ /___/ \
\ \ \ ___________ \ \ \ \ \ /
\_\ \ / /\ \ \ \ \___\/
\ \/ / \ \ \ \ /
\_____/ / \ \ \________\/
/__________/ \ \ /
\ _____ \ /_____\/
\ / /\ \ /___\/
/____/ \ \ /
\ \ /___\/
\____\/
------------------------------------------------------------------------
CP0919MT238=>
我在用户名:"后按回车,然后在密码:"后按回车键.
I pressed return after "Username :" and then after "Password :".
推荐答案
大声笑,我和你的路由器几乎一样.
Lol, i had pretty much the same router as you.
试试这个,我的旧代码:
Try this, bit of my old code:
tn = telnetlib.Telnet(HOST)
tn.read_until('Username : ')
tn.write(user+ "\r")
tn.read_until("Password : ")
tn.write(password+ "\n")
tn.write("\r")
这是针对 Python 2 的,但请尝试在分号后添加额外的空格.此外,如果这不起作用,请使用 wireshark 并查看腻子连接正在做什么并更正您的代码以匹配.
This is for Python 2, but try just adding the extra space after the semicolon. Also, if this does not work, use wireshark and see what the putty connection is doing and correct your code to match.
这篇关于Python Telnet 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!