1

In my expect script I am using exp_continue to send the spacebar until I get a different prompt, however it appears the way I'm doing it might be putting the $expect_out(buffer) outside of the scope of my script.

The script is

#!/usr/bin/expect -f

# Set variables
set timeout 300
set SWITCH [lindex $argv 0]

# Its busy time
spawn ssh -o "StrictHostKeyChecking no" $SWITCH -luser
match_max 100000000
expect {
    "*assword:"
}
send "password\r"
expect "*>"
send "show run\r"
expect {
    "*More*" {send -- " ";exp_continue}
    "*>" {send -- "exit\r"}
}
set fid [open $SWITCH.conf w+]
set out $expect_out(buffer)
puts $fid $out

and the output to my shell shows the complete output (what i would expect to be written to file, below is snipped)

    no telnet server
    username admin password .....
    username user privilege 5 password .....
    !
    !
    hitless-failover enable
    !
    end

    SSH@TELCO-STACK>

however the file that gets written from the puts line only contains the following

^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H
!^M
!^M
!^M
end^M
^M
SSH@TELCO-STACK>

why are only the ^H and the last few lines of output being written to file, when I see the entire (correct) output to stdout?

1
  • All the ^H characters are produced by the application, usually as formatting codes (bold, etc) Oct 12, 2016 at 18:08

1 Answer 1

3

The expect man page says:

Upon matching a pattern (or eof or full_buffer), any matching and previously unmatched output is saved in the variable expect_out(buffer).

This means that you'll only get the characters since the last "More".

I suspect you may want the log_file command instead:

#!/usr/bin/expect -f
set timeout 300
set SWITCH [lindex $argv 0]
spawn ssh -o "StrictHostKeyChecking no" $SWITCH -luser
match_max 100000000
expect "*assword:"
send "password\r"
expect "*>"

log_file /some/log/file

send "show run\r"
expect {
    "*More*" {send -- " ";exp_continue}
    "*>" {send -- "exit\r"}
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .