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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 17:35:28

问题


When I run echo "passphrase" | expect expect.exp "hostname", everything works fine, but expect exits immediately.

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

回答1:


interact does not work when stdin is not a tty. You can pass the password as a command line option, just like the hostname.




回答2:


You might want to use the environment to pass the passphrase:

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

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


来源:https://stackoverflow.com/questions/57842683/how-can-i-prevent-expect-from-exiting-at-eof-when-reading-data-from-stdin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!