如何用expect命令创build一个else语句?

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

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

这个脚本在我第一次进入设备的时候工作的很好,但是如果我第一次进入设备的时候会抛出一个错误

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

大概是因为它继续期待的stringAre you sure you want to continue connecting (yes/no)?

如果该消息不显示,我如何才能告诉脚本进行interact

只有在命令提示符之前使用expect_before才能匹配“未知主机”问题:

 #!/usr/bin/expect spawn ssh user@domain expect_before "*(yes/no)?" { send "yes\r" } expect "*# " interact 

尝试这个:

 #./test.exp 1.1.1.1 

test.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 } }