简体   繁体   English

Expect_out(buffer)未分配

[英]expect_out(buffer) not being assigned

I need some help with a expect script I'm writing to get information from a router I can't reach directly. 我在编写期望脚本时需要一些帮助,以从无法直接到达的路由器获取信息。 This is my code: 这是我的代码:

    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
)

I'm expecting to save that output to a bash variable for further manipulation. 我期望将输出保存到bash变量中,以进行进一步操作。 If I enable exp_internal 1 in the script it does assign what I want to expect_out(buffer) . 如果我在脚本中启用exp_internal 1 ,则它确实分配了我想要的Expect_out(buffer) Can you tell me if I'm doing something wrong? 你能告诉我我做错了什么吗?

Thanks! 谢谢!

Because the heredoc is unquoted, the shell is expanding $expect_out to the empty string before expect launches. 因为heredoc是未引用的,所以shell会期望启动之前$expect_out扩展$expect_out空字符串。 Then expect only sees set output (buffer) 然后期望只看到set output (buffer)

Quote the heredoc: 引用heredoc:

expect=$(
    expect << 'EOD'
    # ........^...^
        ... expect code
EOD
)

See https://www.gnu.org/software/bash/manual/bashref.html#Here-Documents 参见https://www.gnu.org/software/bash/manual/bashref.html#Here-Documents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过expect获得shell中的$expect_out(buffer)和$expect_out(0,string)值 - unable to get the $expect_out(buffer) and $expect_out(0,string) values in shell with expect 在 Bash 脚本中期望:expect_out 不打印出缓冲区 - Expect in Bash script: expect_out not printing out buffer 在Expect脚本中,grep的输出未保存在Expect_out(buffer)中 - In expect script, output of grep is not saved in expect_out(buffer) Linux Expect Expect_out(buffer)不包含任何内容 - Linux Expect expect_out(buffer) does contain nothing 如何存储从Expect_out(buffer)到tcl中的变量的行 - How to store a line from expect_out(buffer) to a variable in tcl 解析Expect_out缓冲区以忽略提示并获取send命令的所需值 - parsing expect_out buffer to ignore the prompt and get the required value of send command 仅在expect_out缓冲区中的提示中存储/检索最后一行 - Only store/retrieve last line from prompt in expect_out buffer Expect脚本如何确定expect_out的内容 - How does Expect script determine contents of expect_out 在使用Expect进行远程登录时,如何通过Expect_out将输出字grep作为下一个命令输入变量? - When telneting with expect, how to grep an output word as the next command input variable via expect_out ? 如果期望条件不满足 RHEL,则退出代码 - To exit out of the code if expect condition not met RHEL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM