我有一个用于 ssh 的期望脚本。我的一些主机会提示输入 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
Run Code Online (Sandbox Code Playgroud)
我需要在 if/then 逻辑中构建的部分是这两行:
expect "Are you sure you want to continue connecting (yes/no)?"
send "yes\r"
Run Code Online (Sandbox Code Playgroud)
逻辑流程是:
if expect "Are you sure you want to continue connecting (yes/no)?"
then send "yes\r"
else
expect "user@$ipaddress's password:"
etc..
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
通过以下代码修改,这对我有用。
expect {
"Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
exp_continue
}
"user@$ipaddress's password:" {
send "password\r"
}
}
Run Code Online (Sandbox Code Playgroud)