By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 2,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

A python telnet entry level question

P: n/a
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/
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

Jinming Xu wrote:
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/


Jul 18 '05 #2

P: n/a
"Jinming Xu" <cy********@hotmail.com> writes:
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()


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
Jul 18 '05 #3

P: n/a
In article <c6**********@scotsman.ed.ac.uk>,
Eddie Corns <ed***@holyrood.ed.ac.uk> wrote:
Jul 18 '05 #4

P: n/a
cl****@lairds.com (Cameron Laird) writes:
In article <c6**********@scotsman.ed.ac.uk>,
Eddie Corns <ed***@holyrood.ed.ac.uk> wrote:
.
.
.
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.


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
Jul 18 '05 #5

P: n/a
In article <c6**********@scotsman.ed.ac.uk>,
Eddie Corns <ed***@holyrood.ed.ac.uk> wrote:
Jul 18 '05 #6

P: n/a
"Andrew Jones" <aj****@sgi.com> wrote in message
news:c6************@fido.engr.sgi.com...
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

Good luck.
Andy

Jinming Xu wrote:
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/

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.