telnetlib problems

V

vercingetorix52

I'm trying to use a python script to access an embedded computer
running linux and connected via a crossover ethernet cable using the
following script...

....and I realize the username and password is not realistic... I'm
still in "proof of concept" stage here :)

#########################
import telnetlib

tn = telnetlib.Telnet('192.168.100.11')

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_very_eager()
#########################

As a script, this doesn't work. However, if I execute the same
commands interactively, it works fine. If I insert some time delays as
follows...

#########################
import telnetlib
import time

tn = telnetlib.Telnet('192.168.100.11')

tn.read_until('login: ', 5)
time.sleep(2)
tn.write('user\n')

tn.read_until('Password: ', 5)
time.sleep(2)
tn.write('password\n')

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

tn.write('ls\n')
time.sleep(2)
print tn.read_very_eager()
#########################

....and it works fine. Can anyone tell me what's going on here? TIA
 
V

vercingetorix52

I just hit upon something that seems to work...

##########################
import telnetlib
from select import select

tn = telnetlib.Telnet('192.168.100.11')
sock = tn.get_socket()

tn.read_until('login: ', 5)
select([sock], [], [], 5)
tn.write('user\n')

tn.read_until('Password: ', 5)
select([sock], [], [], 5)
tn.write('password\n')

tn.read_until('bash-2.05$ ', 5)
tn.write('ls\n')
select([sock], [], [], 5)
print tn.read_very_eager()
##########################

If anyone sees any potential problems with this, I would appreciate it.
TIA
 
E

Eddie Corns

I'm trying to use a python script to access an embedded computer
running linux and connected via a crossover ethernet cable using the
following script...
...and I realize the username and password is not realistic... I'm
still in "proof of concept" stage here :)
#########################
import telnetlib
tn = telnetlib.Telnet('192.168.100.11')
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_very_eager()

As a script, this doesn't work. However, if I execute the same
commands interactively, it works fine. If I insert some time delays as
follows...

What doesn't work about it? Have you checked the return value from the
read_until()s to see if they're returning anything sensible? are any of them
timing out?

Anyway, my first guess would be the use of read_very_eager(), it's something
you normally only descend to when you're stuck for something to match on.
Normally you would do another read_until() for the prompt.

Eddie
 
V

vercingetorix52

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.

Any ideas?
 
E

Eddie Corns

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.read_until('login: ', 5)

tn.read_until('Password: ', 5)

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top