bash: does expect work with multiple password prompts?

a 夏天 提交于 2019-12-10 10:05:37

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/11606149/bash-does-expect-work-with-multiple-password-prompts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!