views:

139

answers:

2

I am running an application, which prompts for a password of the user about a dozen times :-( I tried using expect to circumvent this issue, and make it run in auto mode, but am unable to get over the issue of the multiple times password, which is not exactly static. Sometimes it asks 4-5 times and sometime around 9-10 times.

Is there a better solution to the problem than what I have given below:

spawn myApp [lindex $argv 0]
expect " password: $"
send "$password\r"
expect {
  " password: $"    send "$password\r"
  "^Rollout Done "
  "^Rollout Updated "
}

With the above solution, I have only been able to catch the password twice and then manually start entering for the rest of the time, is there a loop possible with the password?

A: 

Expect can use loops -- it is just TCL with some added commands I believe. So just do

set found 0
while {$found < 1}
{
expect {
 " password: $"    send "$password\r"
 "^Rollout Done "  set found 1
 "^Rollout Updated " set found 1
}
}
Jacob B
you need {braces} around your actions: {set found 1}, {send "$password\r"}
glenn jackman
+1  A: 

Look up the exp_continue command -- it prevents the current [expect] command from returning, so it can find any subsequent password prompts.

spawn myApp [lindex $argv 0]
expect {
    -re { password: $} {
        send "$password\r"
        exp_continue
    }
    -re {^Rollout (?:Done|Updated) }
}

If you want the user to enter the password, rather than storing it in plain text in the script, see How can I make an expect script prompt for a password?

glenn jackman