Python Telnet 端口 23 与 223 具有不同的结果
当我运行此 Python 3.1 代码以使用 telnetlib 访问设备时,它按预期工作:
import telnetlib
tn = telnetlib.Telnet("15.39.100.126", "23")
tn.write(b"menu\n")
tn.write(b"0\n")
print(tn.read_all().decode('ascii'))
然后,我运行此代码(与上面非常相似,但此端口呈现不同的菜单)到端口 223,但什么也没得到:
import telnetlib
tn = telnetlib.Telnet("15.39.100.126", "223")
tn.write(b"ipconfig\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))
运行 telnet 会话时手动发送到 223,它会报告以下内容:
WinCEPocket CMD v 6.00
\>
有没有人曾经在同一设备但不同端口上使用 Python 遇到过类似的不同 telnet 行为的情况,或者有人知道我需要对 WinCE Pocket 采取什么特殊方法吗?端口 23 不使用 WinCE Pocket - 只有端口 223 可以。两个 telnet 程序在同一个 Windows 命令 shell 中运行得同样好。
When I run this Python 3.1 code to access a device using telnetlib, it works as expected:
import telnetlib
tn = telnetlib.Telnet("15.39.100.126", "23")
tn.write(b"menu\n")
tn.write(b"0\n")
print(tn.read_all().decode('ascii'))
Then, I run this code (very similar to above, but this port presents different menus) to port 223 and get nothing:
import telnetlib
tn = telnetlib.Telnet("15.39.100.126", "223")
tn.write(b"ipconfig\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))
When running the telnet session to 223 manually, it reports this:
WinCEPocket CMD v 6.00
\>
Has anyone ever run into something like this with different telnet behavior with Python on the same device but different ports, or does anyone know what special approach I need to take with WinCE Pocket? Port 23 does NOT use WinCE Pocket - only port 223 does. Both telnet programs run equally well from the same windows command shell.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
telnet 命令本身对端口 23 与其他端口执行不同的操作:主要是实现 telnet 选项协商。
telnetlib
的目的是为您实现 Telnet 协议(选项协商等),以便您可以与端口 23 上期望另一端telnet
的服务进行交互。由于telnet
该命令不会对端口 223 执行这些操作,因此您的设备可能不期望 telnet 选项协商,并且被telnetlib
的初始化(发送 telnet 选项)所迷惑带内)。解决方案是使用普通的
socket
与端口 223 交互。SO 上已经有很多这样的例子了。The
telnet
command itself does different things for port 23 vs other ports: Primarily it implements telnet option negotiation. The purpose oftelnetlib
is to implement the Telnet protocol (option negotiation etc) for you so that you can interact with a service on port 23 that expectstelnet
on the other end. Sincetelnet
the command does not do these things for port 223 it's likely that your device is not expecting telnet option negotiation and is being confused by the initialization oftelnetlib
(sending telnet options in-band).The solution would be to use plain
socket
to interact with port 223. There are lots of examples of that on SO already.