How can I make an expect script prompt for a password?

Use expect’s stty command like this:

# grab the password
stty -echo
send_user -- "Password for $user@$host: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)

#... later
send -- "$pass\r"

Note that it’s important to call stty -echo before calling send_user — I’m not sure exactly why: I think it’s a timing issue.

expect programmers should all read the book: Exploring Expect by Don Libes

Leave a Comment