Search code examples
tclbufferexpect

TCL - expect buffer cuts first lines of output


.I made a simple TCL/expect script to capture via regexp some mac-addresses from a command that I usually run on our Wireless Access Controller.

The thing that I dont understand is that I always miss the first 14-15 lines of command's output from the buffer.

#!/usr/bin/expect
log_file -a -noappend wlac.log
#global spawn_id
set timeout 5
#set exp_internal 1
#exp_internal -f debug_wlac.log 1
match_max 700000 

set userf userfile
#username

set pwdf pwdfile
#password

set commands "sta-all"
#commands = display station all

set hostsls "wlac"
#hostsls= hostnames list

set f [open $userf]
set user [read $f]
close $f

set f [open $pwdf]
set password [read $f]
close $f 

set f [open "$hostsls"]
set hosts [split [read $f] "\n"]
close $f

set f [open "$commands"]
set commands [split [read $f] "\n"]
close $f

foreach host $hosts {
    if {[string trim $host] eq ""} then continue
    send_user "\n"
    send_user ">>>>>  Working on $host @ [exec date] <<<<<\n"
    send_user "\n"
    spawn ssh -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o AddKeysToAgent=yes "$user@$host"
    expect {
    "assword"
    {
    send "$password\n"
    }
    ">" 
    {
    send \n
    }
    }

    foreach cmd $commands {
    expect {
    ">"
    {
    append  maclist $expect_out(buffer)
    send "$cmd\n"
    }
    "More" 
    {
    append maclist $expect_out(buffer)
    send "\n"
    exp_continue 
    }
    }
    }
    }
    set pattern_mac {[0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}}
    set match_maclist [regexp -all -inline $pattern_mac $maclist]   
    puts "\n"
    puts "***Beginning of Output***"
    puts "\n"
    puts $maclist
    puts "\n"
    puts "***print array elements***"
    puts "\n"
    foreach i $match_maclist { puts $i }
    puts [llength $match_maclist]

    expect ">"
    send "quit\n"
    close
    wait
    send_user "\n"

I see from the logs, that I always miss this part, that is more or less the first 14 lines of output after the command "display station all" The rest of the output buffer has everything that I expect to be there. I tried enabling the debug log, but I didn't notice anything strange.

<wlac-hostname>display station all
Rf/WLAN: Radio ID/WLAN ID                                                     
Rx/Tx: link receive rate/link transmit rate(Mbps)                             
---------------------------------------------------------------------------------------------------------------------------------------
STA MAC          AP ID AP name                       Rf/WLAN  Band  Type  Rx/Tx      RSSI  VLAN  IP address   SSID                                     
---------------------------------------------------------------------------------------------------------------------------------------
aaaa-bbbb-cc11   1111  AP-HOSTNAME                   1/1      5G    11n   130/117    -55   1111  1.1.1.1      ssid-name
aaaa-bbbb-cc11   1111  AP-HOSTNAME                   1/1      5G    11n   130/117    -55   1111  1.1.1.1      ssid-name
aaaa-bbbb-cc11   1111  AP-HOSTNAME                   1/1      5G    11n   130/117    -55   1111  1.1.1.1      ssid-name
aaaa-bbbb-cc11   1111  AP-HOSTNAME                   1/1      5G    11n   130/117    -55   1111  1.1.1.1      ssid-name
aaaa-bbbb-cc11   1111  AP-HOSTNAME                   1/1      5G    11n   130/117    -55   1111  1.1.1.1      ssid-name
aaaa-bbbb-cc11   1111  AP-HOSTNAME                   1/1      5G    11n   130/117    -55   1111  1.1.1.1      ssid-name
aaaa-bbbb-cc11   1111  AP-HOSTNAME                   1/1      5G    11n   130/117    -55   1111  1.1.1.1      ssid-name
aaaa-bbbb-cc11   1111  AP-HOSTNAME                   1/1      5G    11n   130/117    -55   1111  1.1.1.1      ssid-name
aaaa-bbbb-cc11   1111  AP-HOSTNAME                   1/1      5G    11n   130/117    -55   1111  1.1.1.1      ssid-name
aaaa-bbbb-cc11   1111  AP-HOSTNAME                   1/1      5G    11n   130/117    -55   1111  1.1.1.1      ssid-name
aaaa-bbbb-cc11   1111  AP-HOSTNAME                   1/1      5G    11n   130/117    -55   1111  1.1.1.1      ssid-name

(ip, hostname, and other stuff are just examples)

After this point, everything is ok. I did a check, and this isn't happening where there is a "---- More ----" but way before.


Solution

  • I suspect the expect input buffer is too small, which is why you have match_max 700000. Unfortunately, this has no effect as it applies to the currently spawned process, of which there is none.

    You need to set at the start the default size, for future spawn commands by using:

    match_max -d 700000