tcl - TCL - 期望缓冲区削减输出的第一行
问题描述
.我制作了一个简单的 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 ----”的地方,而是以前的地方。
解决方案
我建议,如果您的目标是获取 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
. 请从期望手册中了解这一点:
请注意,过大的值 [of
match_max
] 会减慢模式匹配器的速度。
推荐阅读
- python - 几乎在那里使用 auto-py-to-exe 冲刺到 .exe
- python - 在python中的列表中添加列表
- google-apps-script - 隐藏的 Google 表格启动脚本?
- python - 为什么不渲染模板
- node.js - 错误:13 内部:序列化失败(使用 Google Cloud Vision Api)
- r - AWS Elastic Beanstalk 上的闪亮服务器与 Docker
- kubernetes - 继承的 EKS 集群,无权运行 kubectl 命令
- scala - 结合两个 EitherT,如果成功则返回第一个,否则返回第二个
- keras - 从 Keras 模型中获取两个输出
- kubernetes - Kubernetes 中的功能,但 Swarm 中没有