从 stdin 读取数据时,如何防止 `expect` 在 EOF 处退出?

How can I prevent `expect` from exiting at EOF when reading data from stdin?

当我 运行 echo "passphrase" | expect expect.exp "hostname" 时,一切正常,但 expect 立即退出。

expect.exp

#!/usr/bin/expect

set passphrase [gets stdin]

set hostname [lindex $argv 0]

spawn ssh admin@$hostname
expect "passphrase"
send "$passphrase\r"
expect "admin@$hostname"
send "clear\r"
interact
stdin 不是 tty 时,

interact 不起作用。您可以将密码作为命令行选项传递,就像主机名一样。

您可能希望使用环境来传递密码:

# shell code
export passphrase="pass phrase"
expect expect.exp hostname

# expect code
...
send "$env(passphrase)\r"