Search code examples
outputexpect

Why does expect-tcl use the semicolon to trim the output?


My script garbage piece of a configuration from the network devices by telnet for further modifications. Some of pieces of configurations contain encoded data with semicolon character. However, in a strange way, expect cuts the output after the first semicolon character, with that all the information gets into the log. For example, the configuration contains the following lines, they are also in the log:

 snmp-agent
 snmp-agent local-engineid 000007DB7F00000100000DBD
 snmp-agent community write cipher %@%@f:x6"^s,6.L~~BE~%c*0S6NH2@Y_W4I`NP6,W}VF'NN86NKSYoixJc$>;88sTj2yu2*/NTS6%@%@
 snmp-agent community read cipher %@%@]$OdG*7WdV@{aSD9vx"DH+]]*_[8D+2\u%7Ozr<,W3zP+]`HBK(\=oJuKL'IT|+w*3o4]iH+%@%@
 snmp-agent sys-info version v1 v2c
 undo snmp-agent sys-info version v3

I tried two different ways, but the result is unchanged:

1)

expect {
    "Error: Failed to authenticate." {exit}
    ">" {send "disp current-configuration | include snmp\r"}
}
expect "$device>" {
    set results [regexp -all -inline {[^\r\n]+} $expect_out(buffer)]
    puts "Length of output buffer is : [llength $results]"
    for {set i 1} {$i<[llength $results]-1} {incr i} {
        set confline [lindex $results $i]
        puts "\$confline\[$i\] = $confline\r"
expect {
    "Error: Failed to authenticate." {exit}
    ">" {send "disp current-configuration | include snmp\r"}
}
expect "$device>" {
    set outfl [open "$SCROOT/$model-$device.out" w]
    puts $outfl $expect_out(buffer)
    flush $outfl
    close $outfl

And this is what happens at the output:

 snmp-agent
 snmp-agent local-engineid 000007DB7F00000100000DBD
 snmp-agent community write cipher %@%@f:x6"^s,6.L~~BE~%c*0S6NH2@Y_W4I`NP6,W}VF'NN86NKSYoixJc$>

Who knows how to solve this problem? Please help me to do it.

UPD: I extended the match condition and got the expected result. Thank Colin Macleod for the hint.


Solution

  • You are trying to read all the data up to the next prompt by doing

    expect ">" {
    

    but the output data contains a ">" character so expect stops when it sees that. It's just coincidence that the next character happens to be ";".

    You can probably work around this by writing your pattern to match ">" only when it is the first non-white-space character on a line, e.g.

    expect "\n\s*>" {