开发者

Python Telnet Port 23 vs 223 with Different Results

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 开发者_如何学Goget 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.


The telnet command itself does different things for port 23 vs other ports: Primarily it implements telnet option negotiation. The purpose of telnetlib is to implement the Telnet protocol (option negotiation etc) for you so that you can interact with a service on port 23 that expects telnet on the other end. Since telnet 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 of telnetlib (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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜