Bash if-then expect脚本中的逻辑

Bash if-then expect脚本中的逻辑,bash,expect,Bash,Expect,我有一个用于ssh的expect脚本。有些主机会提示RSA主机密钥文本,有些则不会。如何在expect中设置if/then脚本。这是我到目前为止所拥有的 #!/usr/bin/expect -f set ipaddress [lindex $argv 0]; spawn ssh user@$ipaddress expect "Are you sure you want to continue connecting (yes/no)?" send "yes\r" expect "user@$ip

我有一个用于ssh的expect脚本。有些主机会提示RSA主机密钥文本,有些则不会。如何在expect中设置if/then脚本。这是我到目前为止所拥有的

#!/usr/bin/expect -f

set ipaddress [lindex $argv 0];
spawn ssh user@$ipaddress
expect "Are you sure you want to continue connecting (yes/no)?"
send "yes\r"
expect "user@$ipaddress's password:"
send "password\r"
set prompt {\$ $}
expect -re $prompt
send "$command\r"
expect -re $prompt
send "quit\r"
interact
我需要在if/then逻辑中构建的部分是以下两行:

expect "Are you sure you want to continue connecting (yes/no)?"
send "yes\r"
逻辑流程为:

if expect "Are you sure you want to continue connecting (yes/no)?"
then send "yes\r"
else
expect "user@$ipaddress's password:"
etc..

我怎样才能做到这一点呢?

通过以下代码修改,我可以做到这一点

expect {
    "Are you sure you want to continue connecting (yes/no)?" {
         send "yes\r"
         exp_continue
    }
    "user@$ipaddress's password:" {
         send "password\r"
    }
    }
如果要“盲目”接受主机密钥,可以执行以下操作:


有几个建议-ssh密钥扫描方法是更安全的方法。

您将
exp\u continue
放在错误的位置:它应该在“确定”块中。因为在这两种情况下你都必须输入密码。更新我的答案以反映你的建议。自从搬家以来,我还没有一次失败,所以你似乎是对的。