473,789 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

telnetlib problems

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.Telne t('192.168.100. 11')

tn.read_until(' login: ', 5)

tn.write('user\ n')

tn.read_until(' Password: ', 5)

tn.write('passw ord\n')

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

tn.write('ls\n' )

print tn.read_very_ea ger()
############### ##########

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.Telne t('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('passw ord\n')

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

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

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

Feb 28 '06 #1
4 10520
I just hit upon something that seems to work...

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

tn = telnetlib.Telne t('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('passw ord\n')

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

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

Feb 28 '06 #2
ve************* @yahoo.com writes:
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.Telne t('192.168.100. 11')
tn.read_until( 'login: ', 5)
tn.write('user \n')
tn.read_until( 'Password: ', 5)
tn.write('pass word\n')
tn.read_until( 'bash-2.05$ ', 5)
tn.write('ls\n ')
print tn.read_very_ea ger()
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
Mar 1 '06 #3
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.Telne t('192.168.100. 11')

tn.set_debuglev el(9)

tn.read_until(' login: ', 5)

tn.write('user\ n')

tn.read_until(' Password: ', 5)

tn.write('passw ord\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\x ff\xfd
\xff\xfd#\xff\x fd'"
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\x ff\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\x01Em bedded 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\nEmbedde d:
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?

Mar 1 '06 #4
ve************* @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.Telne t('192.168.100. 11') tn.set_debugle vel(9) tn.read_until( 'login: ', 5) tn.write('user \n') tn.read_until( 'Password: ', 5) tn.write('pass word\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\x ff\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\xf b\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\x01Em bedded 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\nEmbedde d:
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
Mar 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4808
by: walala | last post by:
Dear all, I recently came across a Python program which used "telnetlib" to automate things in the several unix machines in our local networks; I attached the script as follows; I wonder if it is possible to have the equivlent libary for "rlogin" in Python? Because our local networks support only SSH or RLOGIN. This script used Telnet so it could not be used in our local networks;
1
2087
by: ÕÅÖ¾Ã÷ | last post by:
code below cannt work import getpass import sys import telnetlib import select tn = telnetlib.Telnet() print tn.open("162.105.31.222",23)
1
1901
by: Stephen Ferg | last post by:
I'm trying to use telnetlib to run a Java program on a remote server. I'm having strange problems, and I'm wondering if anyone might be able to help. I have two UNIX servers, A and Z. I have an application named LATTE that runs on server A. When the user starts LATTE, the user can choose to create a telnet connection back to A, or to Z. Then LATTE uses the telnet connection to run a Java program (run it several times, on different...
3
6460
by: Mike Monaghan | last post by:
I'm rather new to python but a long time programmer. I think I've covered my bases so I hope this isn't somthing obvious. I'm running ActiveState PythonWin 2.3.2 (#49, Nov 13 2003, 10:34:54) on win32. I have several processes that are polling a telnet server on a regular basis. I'm using telnetlib for this. When I issue the .close method the connection is not closed (no TCP FIN is sent to the server).
3
4983
by: Svha | last post by:
Greetings I'd like to do some tweaks on a telnet session as I have no control over the server side what so ever. However I cannot correctly negotiate a telnet connection to the server. I've gone back to basics on the code for simplicity here; Linux 2.6 : Python 2.3.4
3
12482
by: Wojciech Halicki-Piszko | last post by:
How to know if connection is active after telnetlib.Telnet.open(host,port)?
5
2858
by: Jerry | last post by:
Can anyone tell me if the telnetlib module is thread-safe? I've done some looking, but don't know, and I don't know how to tell from reading the module code.
0
1086
by: Matthew Warren | last post by:
Hi, I use telnetlib in an app I am writing, and would like to add functionality to it to support interactive terminal sessions , IE: be able to 'vi' a file. Currently it seems telnetlib isnt quite sophisticated enoguh to support such a thing. The trouble is, I havent got a clue where to start and would appreciate
3
3250
by: Phoe6 | last post by:
Hi All, I am trying to use the telnetlib module. Manually when I do telnet 172.31.128.244 I get: Login: (I type root) Password: ( I type Password) And it enters to the Telnet Session:
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9499
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5404
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4076
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2898
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.