2

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.

Thom Ives
  • 3,642
  • 3
  • 30
  • 29

1 Answers1

3

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.

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
  • Ben, Thanks for the answer, but I am not sure how to implement what you are saying toward a possible solution. Can you elaborate? Thom – Thom Ives Oct 14 '11 at 22:47
  • Ben, Thanks much for the update. That makes sense with my experience. In fact, I had tried some socket programming with this port too, but so far, I have not been able to get decipherable output from the socket code. I will keep trying and maybe post a question on the socket code. Thom – Thom Ives Oct 17 '11 at 16:28