arvind
arvind

Reputation: 113

Writing Expect Script for random questions in while loop

I am trying to write an expect script with a while loop where I have a set of questions that will be asked randomly again and again. I have created a script for the same but that is not working as expected. Is it important that all the expectations in the expect block should be in the sequence? Also, Is it the correct way of exiting the while loop in case of success?

#!/usr/bin/expect -f  
set timeout 1  
spawn ./AnotherFile.sh

while {1} {

        expect {
                {Enter password:}                                               {send -- "Password\r";exp_continue}
                {Trust this certificate? [no]:}                                 {send -- "yes\r";exp_continue}
                {Enter pass phrase:}                                            {send -- "Password\r";exp_continue}
                {Verifying - Enter pass phrase:}                                {send -- "Password\r";exp_continue}
                {Country Name (2 letter code) [AU]:}                            {send -- "IN\r";exp_continue}
                {State or Province Name (full name) [Some-State]:}              {send -- "XX\r";exp_continue}
                {Locality Name (eg, city) []:}                                  {send -- "XXXXX\r";exp_continue}
                {Organization Name (eg, company) [Internet Widgits Pty Ltd]:}   {send -- "XXXXX\r";exp_continue}
                {Organizational Unit Name (eg, section) []:}                    {send -- "XXXXX\r";exp_continue}
                {Common Name (e.g. server FQDN or YOUR name) []:}               {send -- "XXXXX\r";exp_continue}
                {Email Address []:}                                             {send -- "\r";exp_continue}
        }
        expect {
                {"Successfully Done."}                {send -- "exit\r"}
        }
}
expect eof  
close $spawn_id

You can also think of my problem in terms of "C" code as:

While(1)
{
    Switch(expect_command)
    {
        case "Enter Password": 
            send "Password\r"
        case "is certificate valid"
            send "yes\r"
        ...
        case "successfull"
            send "exit\r"
    }
}

Upvotes: 1

Views: 257

Answers (1)

glenn jackman
glenn jackman

Reputation: 247062

Put "Successfully Done" in the same expect command as the others. There should be at least one pattern that will not exp_continue so the expect command can actually end (without timing out).

Also, since you're in a while 1 loop, you need to send "exit\r"; break

Upvotes: 1

Related Questions