telnetlib problems

Eddie Corns eddie at holyrood.ed.ac.uk
Thu Mar 2 12:40:25 CET 2006


vercingetorix52 at yahoo.com writes:

>Thanks for the reply. I've replaced the call to read_very_eager() with
>read_until() and enabled debugging messages.  My script now looks like
>this...

>#############################
>import telnetlib

>tn = telnetlib.Telnet('192.168.100.11')

>tn.set_debuglevel(9)

>tn.read_until('login: ', 5)

>tn.write('user\n')

>tn.read_until('Password: ', 5)

>tn.write('password\n')

>tn.read_until('bash-2.05$ ', 5)

>tn.write('ls\n')

>print tn.read_until('bash-2.05$ ', 5)
>#############################

>Each call to read_until() returns a valid string except for the second
>one, i.e.,
>tn.read_until('Password: ', 5) returns a null string.

>Here's the program output with debugging enabled...
>#############################
>Telnet(192.168.100.11,23): recv "\xff\xfd\x18\xff\xfd
>\xff\xfd#\xff\xfd'"
>Telnet(192.168.100.11,23): IAC DO 24
>Telnet(192.168.100.11,23): IAC DO 32
>Telnet(192.168.100.11,23): IAC DO 35
>Telnet(192.168.100.11,23): IAC DO 39
>Telnet(192.168.100.11,23): recv
>'\xff\xfb\x03\xff\xfd\x01\xff\xfd\x1f\xff\xfb\x05\xff\xfd!'
>Telnet(192.168.100.11,23): IAC WILL 3
>Telnet(192.168.100.11,23): IAC DO 1
>Telnet(192.168.100.11,23): IAC DO 31
>Telnet(192.168.100.11,23): IAC WILL 5
>Telnet(192.168.100.11,23): IAC DO 33
>Telnet(192.168.100.11,23): recv '\xff\xfb\x03'
>Telnet(192.168.100.11,23): IAC WILL 3
>Telnet(192.168.100.11,23): recv '\xff\xfb\x01Embedded Systems, 2050
>Single Board Computer.\r\nR'
>Telnet(192.168.100.11,23): IAC WILL 1
>Telnet(192.168.100.11,23): recv 'uning \\s Kernel \\r.\r\nEmbedded:
>2050 Special Feature'
>Telnet(192.168.100.11,23): recv 's Enabled.\r\n\r\n'
>Telnet(192.168.100.11,23): recv 'login: '
>Telnet(192.168.100.11,23): send 'user\n'
>Telnet(192.168.100.11,23): send 'password\n'
>Telnet(192.168.100.11,23): recv 'Password: '
>Telnet(192.168.100.11,23): send 'ls\n'
>Telnet(192.168.100.11,23): recv '\r\n'
>Telnet(192.168.100.11,23): recv 'Login incorrect\r\n\r\nlogin: '


>Login incorrect



>login:
>#############################
>It looks like it's sending the password before receiving the password
>prompt.

It looks more like it's taking longer than 5 seconds to receive the Password:
prompt so it times out, returns to your code and you send the next string
anyway but (assuming this is a valid password) the far end might throw away
any pending input when it finally does send the Password: prompt (otherwise it
would probably have got back in sync).

In short you shouldn't go past each stage unless you've verified that you got
the expected response.  I can't actually remember exactly what the responses
signify but at a minimum I assume the empty string means there's no point in
continuing because what you expected to see wasn't there so you need to either
abort or try some recovery process.  And also make sure you give it a
reasonable time to respond.

Eddie



More information about the Python-list mailing list