我需要有关正在编写的期望脚本的帮助,以从无法直接到达的路由器获取信息。这是我的代码:
expout=$(
expect << EOD
log_user 0
set timeout 20
spawn ssh router1
expect "router1#" { send "ssh -l foo 0.0.0.0\r" }
expect "Password:" { send "*********\r" }
expect "router2#" { send "show ip interface br\r \r" }
set output $expect_out(buffer)
expect "router2#" { send "exit\r" }
expect "router1#" { send "exit\r" }
puts $output
expect eof
EOD
)
我希望将输出保存到bash变量中以进行进一步的操作。如果我在脚本中启用exp_internal 1
,则会确实将我想要的内容分配给 expect_out(buffer)。你能告诉我我做错了什么吗?
谢谢!
答案 0 :(得分:1)
由于未引用heredoc,因此外壳程序将$expect_out
扩展为空字符串 。然后期望只会看到set output (buffer)
引用heredoc:
expect=$(
expect << 'EOD'
# ........^...^
... expect code
EOD
)
请参见https://www.gnu.org/software/bash/manual/bashref.html#Here-Documents