期望不等待生成的进程

Expect not waiting for spawned process

我正在尝试编写 expect 脚本来启动 SSH 隧道,等待隧道进程关闭(抛出错误),然后重新运行隧道命令。 (值得注意的是,autossh 和其他自动隧道解决方案与此处无关,因为我要做的涉及 expect。)这是带有故障排除消息的完整脚本。

#!/usr/bin/expect

set timeout 1

while { $timeout == 1 } {
  spawn -noecho ssh -f -N -T -R22224:localhost:22223 username@xxx.xxx.xxx.xxx
  send_user "Looking for host"
  set timeout 0
  wait
  set timeout 1
  send_user "We've encountered an error. Re-running."
}

send_user "Out of the loop."

ssh 命令有效,expect man page 表示 wait 应该保留脚本直到当前派生的进程结束,但它没有,它最终重新运行循环很多次(迫使我在我的远程机器上 pkill 许多 sshd 个实例)。我试过 wait spawnwait spawn_idexpect {ssh*}(根据手册页,这也应该让脚本等待),但没有什么会使脚本暂停。

ssh -f 将派生一个子进程(运行 在后台)并且父进程简单地退出。 Expect 的 wait 命令只等待 spawned 父进程,所以 wait 很快就会 return。