expect help

Greetings

I have a login process that I want to cover multiple possible prompts.

I.E. I have the initial prompt asking for user and then if the user exists asks for the password.

However I would like to take into account the possibility that the user is invalid (and an invalid password).

So reading the man page I saw the following:

expect {
                            connected           break
                            busy                {print busy\n ; continue}
                            failed              abort
                            "invalid password"  abort
                            timeout             abort
                     }

Which I was hoping was an example of looping and processing the input strings as they come up.

As I write this, that is what I am looking for. A way to code different strings that could possibly come up and have actions based on those strings.

Is something like that possible ?

Sure. Here's a quick snippet using regular expressions and builtin error indices.


proc id_pat_match {p} {
                                  send_user  "Saw pattern $p\n..continuing"
                                  exp_continue
}

set prompt1 "(.*@.*> | .*> | .*\$) " 
set prompt2 ".*#"
spawn ssh $user@$host
expect  {   
             -re $prompt1    {id_pat_match $prompt1}
             -re $prompt2    {id_pat_match $prompt2 }
             timeout {send_user "<Timed out...>"}
             eof {send_user "<eof...>"}
}

HTH.