我试图让一个期望脚本通过 ssh 进入远程系统,并获取远程命令的输出。我希望在本地提供该输出。例如,通过 ssh 进入系统并获取本地时间输出:
#!/usr/bin/expect --
set timeout 60
spawn ssh user@server.domain.com
expect {
timeout {puts "timed out after $timeout seconds"; exit}
"Password:"
}
send "SuperSecretPassword\r"
expect "user$ "
send "date\r"
expect "date"
expect "\r"
set buffer1 '$expect_out(buffer)'
expect "\r"
set buffer2 '$expect_out(buffer)'
send "exit\r"
expect eof
set buffer3 '$expect_out(buffer)'
exit
export VARIABLE $buffer1
export VARIABLE $buffer2
export VARIABLE $buffer3
echo "Okay, we are done expecting..."
echo "Buffer1: $buffer1"
echo "Buffer2: $buffer2"
echo "Buffer3: $buffer3"
自然,上述方法不起作用,实际上脚本执行在期望“退出”之后结束