如果然后逻辑内部期望脚本

时间:2016-07-22 20:15:46

标签: bash expect

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

#!/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..

我该如何做到这一点?

2 个答案:

答案 0 :(得分:1)

通过以下代码修改,这对我有用。

expect {
    "Are you sure you want to continue connecting (yes/no)?" {
         send "yes\r"
         exp_continue
    }
    "user@$ipaddress's password:" {
         send "password\r"
    }
    }

答案 1 :(得分:0)

如果您要“盲目地”接受主机密钥,您可以执行类似于此处建议的操作:

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

有一些建议 - ssh-keyscan方法是更安全的方式。