TCL - 期望缓冲区削减输出的第一行

TCL - expect buffer cuts first lines of output

。我制作了一个简单的 TCL/expect 脚本,通过正则表达式捕获来自命令的一些 mac 地址,我通常在我们的无线访问控制器上 运行。

我不明白的是,我总是错过缓冲区中命令输出的前 14-15 行。

#!/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"

我从日志中看到,我总是错过这部分,或多或少是命令“display station all”后输出的前 14 行 输出缓冲区的其余部分包含我期望的所有内容。 我尝试启用调试日志,但没有发现任何异常。

<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、主机名和其他内容只是示例)

到此为止,一切正常。 我检查了一下,这并没有发生在有“---- More ----”的地方,而是很久以前。

我怀疑期望的输入缓冲区太小,这就是你 match_max 700000 的原因。不幸的是,这没有效果,因为它适用于当前生成的进程,其中有 none.

您需要在开始时设置 默认 大小,以便将来生成命令使用:

match_max -d 700000

我建议,如果您的目标是获取 MAC 列表,则不需要累积整个输出:

# ... initial stuff

set maclist {}

foreach host $hosts {
    if {[string trim $host] eq ""} then continue

    # using the builtin `timestamp` command
    send_user "\n>>>>>  Working on $host @ [timestamp -format %c] <<<<<\n\n"

    spawn ssh -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o AddKeysToAgent=yes "$user@$host"

    expect {
        "assword" { send "$password\n"; exp_continue }
        ">"
    }

    foreach cmd $commands {
        send "$cmd\r"
        expect {
            -re {\y[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}\y} {
                lappend maclist $expect_out(0,string)
                exp_continue 
            }
            "More" {
                send "\n"
                exp_continue 
            }
            ">" 
        }
    }
}

puts ""
puts "MAC list"
puts [join $maclist \n]

这意味着您不需要更改 match_max。请从 expect 手册中了解这一点:

Note that excessively large values [of match_max] can slow down the pattern matcher.