期待:回答一个或两个ssh密码提示然后进行交互

时间:2017-06-08 18:22:01

标签: ssh passwords expect

我有一个ssh案例(不能使用sshkeys),有时会提示输入一个密码,有时会提示两个密码(两种情况下提示相同)。我尝试过while,if,exp_continue的变体,但似乎无法正确烹饪。

如果我只收到一个提示,这就完美了:

#!/usr/bin/expect
set pwd "mypwd"
set prompt "*Password*"
set uid "*userid*"
set timeout -1
spawn -noecho ssh -q host.domain

expect $prompt
send $pwd
send \r
interact

试过这个,但没有给我提供我需要的结果,有时两个pwds加上KnownHost案例。

expect {
    "*yes/no*" { send \"yes\r\"; exp_continue }
    $prompt { send \"$pwd\r\"; exp_continue }
    $uid {interact}
}

1 个答案:

答案 0 :(得分:1)

想出来:

#!/usr/bin/expect -f

set pwd "mypwd"
set prompt "Enter PIN for 'PIV_II (PIV Card Holder pin)': "
set uid "*userid*"
set timeout -1
spawn ssh -q host.domain

expect {
    "*yes/no*" { send "yes\r"; exp_continue }
    $prompt { send "$pwd\r"; exp_continue }
    $uid { send \r; interact }
}