telnet session

Eddie Corns eddie at
Wed Mar 29 14:06:05 CEST 2006

eight02645999 at writes:

>i am using a telnet session to simulate an authentication mechanism
>USER = "user"
>PASSWORD = "password"
>        telnet = telnetlib.Telnet(HOST)
>        telnet.set_debuglevel(5)
>        telnet.read_until("login: ")
>        telnet.write(USER + "\n")
>        telnet.read_until("Password: ")
>        telnet.write(PASSWORD + "\n")
>        print "failed to telnet"
>        try:
>                telnet.write("ls\n")
>        except:
>                print "cannot ls"
>        else:
>                telnet.write("exit\n")
>                print telnet.read_all()

>When i purposely input a wrong password, it "hangs" at the login prompt
>waiting for
>login and Password. The host i am telnetting to is a unix server.
>How can i "exit" this login prompt if the user keys in wrong password
>in my script?

Either do an explicit read_until() for the prompt and fail if it times out or
use expect() to see whether you saw "Password:" or prompt.  If you absolutely
don't know what the prompt might be I suppose you could do another
read_until("Password:") and if it times out then assume you got through.

More information about the Python-list mailing list