bash: does expect work with multiple password prompts?

bash, expect, passwords, ssh

Solution

You have to do a little programming. Fortunately the answer is pretty simple:

#!/bin/sh

PASS=MyPassword
export PASS

/usr/bin/expect -c '
    spawn python Tools/python/install.py
    expect {
        -nocase "password:" {
            send "$env(PASS)\r"
            exp_continue
        }
        "somthing_else_that_indicates_you're_ready_to_interact"
    }
    interact
'

Cleaned up the shell quoting a little too.

Problem

I am currently using expect to pass in passwords so my scripts can run automatically without me having to sit around and type in the same password over and over again. Important: Please don't comment about how big of a security risk this is or how I should be using ssh keys, I would use those if I could, but the setup I have to work with doesn't allow it. My code looks like the following: ``` #!/bin/sh PASS=mypassword /usr/bin/expect -c " spawn python Tools/python/install.py expect -nocase \"password:\" {send \"$PASS\r\"; interact} " ``` The problem I have is that install.py prompts for the same password a dozen times, it appears expect only auto fills the password for the first prompt. Is there a way to modify the behavior so that it fills in the password all 12 times and not just the first time?

Original source