如何使用expect 命令创建else 语句?

use*_*637 5 linux

我有以下期望脚本,如果已知主机不存在,它会添加一个已知主机。

#!/usr/bin/expect
spawn ssh user@domain "cd /home/user"
expect "Are you sure you want to continue connecting (yes/no)?"
send "yes\r"
interact
Run Code Online (Sandbox Code Playgroud)

该脚本在我第一次通过 ssh 连接到设备时运行良好,但是如果我通过 ssh 连接到设备 n + 1 次,则会引发错误

send: spawn id exp6 not open
    while executing
"send "yes\r""
    (file "./testing_spawn.sh" line 4)
Run Code Online (Sandbox Code Playgroud)

大概是因为它继续期待字符串 Are you sure you want to continue connecting (yes/no)?

interact如果该消息没有显示,我如何告诉脚本?

cho*_*ple 0

尝试这个:

#./test.exp 1.1.1.1
Run Code Online (Sandbox Code Playgroud)

测试.exp

#!/usr/bin/expect -f
set ip [lindex $argv 0]

spawn ssh -q $ip
expect {
  timeout { send_user "\nFailed\n"; exit 1 }
  eof { send_user "\nSSH failure for $ip\n"; exit 1 }
  "Password:" {
    send "password\n"
  }
  "*(yes/no)?*" {
    send "yes\n"
    interact
  }
  "*#" {
    interact
  }
}
Run Code Online (Sandbox Code Playgroud)