A python telnet entry level question

Eddie Corns eddie at holyrood.ed.ac.uk
Thu Apr 22 06:11:23 EDT 2004


"Jinming Xu" <cybermanxu at 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



More information about the Python-list mailing list