telnet

Eddie Corns eddie at holyrood.ed.ac.uk
Wed May 1 06:59:32 EDT 2002


"Billy Ng" <kwokng at earthlink.net> writes:

>Hi folks,

>I am writing a python app that uses the Telnet object to connect to the
>server.  Here is the code,

>tn = telnetlib.Telnet(host)
>tn.read_until("Please enter your id")
>tn.write(userid+"\r\n")

>From here, tn will return either "Please enter your password" or "Wrong id,
>please enter your id again".  My question is how can I get the returning
>string before I do the next tn.write()

>Thanks!

>Billy Ng

I think you need to use tn.expect here.  I've not tried it yet but I guess it
goes along the lines of:

resp = tn.expect (["password","id again"])
if resp[0] == 0:   # wants a password
  tn.write (pass)
else:              # oops
  ...

In reality you'd want to specify the strings a bit more carefully.

Eddie



More information about the Python-list mailing list