问题
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