如果那么期望脚本中的逻辑

if then logic inside expect script

我有一个用于 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"
    }
    }

如果您要"blindly"接受主机密钥,您可以按照此处的建议进行操作:

https://askubuntu.com/questions/123072/ssh-automatically-accept-keys

有几个建议 - ssh-keyscan 方法是更安全的方法。