问题描述
.我制作了一个简单的 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,主机名和其他东西只是示例)
此后,一切正常。 我做了一个检查,这不会发生在有“---- 更多----”的地方,而是发生在之前。
解决方法
我怀疑预期输入缓冲区太小,这就是为什么您有 match_max 700000
。不幸的是,这没有任何效果,因为它适用于当前没有生成的进程。
您需要在开始时设置默认大小,以便使用:
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 手册中的这一点:
请注意,过大的值 [of match_max
] 会减慢模式匹配器的速度。