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. If I enable exp_internal 1 in the script it does assign what I want to expect_out(buffer). Can you tell me if I'm doing something wrong?

Thanks!

Upvotes: 0

Views: 189

Answers (1)

glenn jackman
glenn jackman

Reputation: 246774

Because the heredoc is unquoted, the shell is expanding $expect_out to the empty string before expect launches. Then expect only sees set output (buffer)

Quote the heredoc:

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

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

Upvotes: 1

Related Questions