A python telnet entry level question

J

Jinming Xu

Hello Everyone,

I am trying to write a python script to telnet to a server and then do
something there. As the first step, I practiced the python example in Lib
Reference 11.13.2. But I am finding the script stops after I supplied the
password. Does anyone know why?


Thanks in advance!

Jinming Xu

PS: Here is the script:

import getpass
import sys
import telnetlib

HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")

print tn.read_all()

_________________________________________________________________
MSN Toolbar provides one-click access to Hotmail from any Web page – FREE
download! http://toolbar.msn.com/go/onm00200413ave/direct/01/
 
A

Andrew Jones

It might be getting stuck on the Password part.

Try this:

tn.read_until("Password: ", 1)


Ethereal can be very helpful. Use it to see what is happening on the
wire. It has a "follow TCP stream" function. Obviously this will not
work for telneting to localhost so you will need to telnet to a remote host.

Good luck.
Andy
 
E

Eddie Corns

Jinming Xu said:
Hello Everyone,
I am trying to write a python script to telnet to a server and then do
something there. As the first step, I practiced the python example in Lib
Reference 11.13.2. But I am finding the script stops after I supplied the
password. Does anyone know why?

Thanks in advance!
Jinming Xu
PS: Here is the script:
import getpass
import sys
import telnetlib
HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")

print tn.read_all()

You are maybe being too specific in what you match for. At least one of my
machines prompts:

Pasword for <username>:

rather than just "Password: ". I tend to match things like

"ogin" (in case of Login vs login)
"assword" (will this get filtered out by censoring s/w?)

Eddie
 
C

Cameron Laird

.
.
.
You are maybe being too specific in what you match for. At least one of my
machines prompts:

Pasword for <username>:

rather than just "Password: ". I tend to match things like

"ogin" (in case of Login vs login)
"assword" (will this get filtered out by censoring s/w?)

Eddie

This sort of tolerance can lead to its own problems (though
I entirely agree you're right to recommend it). Some logins
are so sensitive to timing (in essence) that matching
"assword" rather than "assword:" results in the telnetd
ignoring the first character or two of response.

So what to do? At this level, there is *no* good answer.
The most enlightened thought is simply to recognize that
telnet forces one into a cascade of heuristic hacks.
 
E

Eddie Corns

This sort of tolerance can lead to its own problems (though
I entirely agree you're right to recommend it). Some logins
are so sensitive to timing (in essence) that matching
"assword" rather than "assword:" results in the telnetd
ignoring the first character or two of response.
So what to do? At this level, there is *no* good answer.
The most enlightened thought is simply to recognize that
telnet forces one into a cascade of heuristic hacks.

And then you get unhelpful router manufacturers that put code in to check
whether passwords are typed too fast (or regularly spaced) and ignore them
because they're obviously not dealing with a human! Took me ages to figure
out why my scripts were failing (then about 10 seconds to defeat it).

Yes, using telnet is more art than science but it's a lot better now than
before we had expect (for heavy duty jobs) and telnetlib (for simpler jobs).

Eddie
 
C

Cameron Laird

.
.
.
And then you get unhelpful router manufacturers that put code in to check
whether passwords are typed too fast (or regularly spaced) and ignore them
because they're obviously not dealing with a human! Took me ages to figure
out why my scripts were failing (then about 10 seconds to defeat it).

Yes, using telnet is more art than science but it's a lot better now than
before we had expect (for heavy duty jobs) and telnetlib (for simpler jobs).

Eddie

I entirely agree. Expect and telnetlib definitely *do*
ease life's burdens.

The examples you cite are common enough that Don Libes,
creator and maintainer of the original Expect, became
sufficiently expert in timing issues to make development
of "drunken typist" applications simple <URL:
http://expect.nist.gov/example/beer.exp >.
 
M

Mark

Andrew Jones said:
It might be getting stuck on the Password part.

Try this:

tn.read_until("Password: ", 1)


Ethereal can be very helpful. Use it to see what is happening on the
wire. It has a "follow TCP stream" function. Obviously this will not
work for telneting to localhost so you will need to telnet to a remote
host.

I just discovered telnetlib yesterday and found "set_debuglevel" to be very
useful to figure out what to read_until.

-Mark
 

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

Similar Threads


Members online

Forum statistics

Threads
473,991
Messages
2,570,212
Members
46,800
Latest member
Tobi1987

Latest Threads

Top