如何搜索 Expect 变量
How can I search through an Expect variable
我正在编写一个连接到交换机然后显示接口配置的 expect 脚本。然后我分析这个输出来检查某些事情。我想存储我正在检查的其中一件事情的输出,我试图通过搜索 $expect_out(buffer) 来完成,尽管我很难找到如何做这个。
我应该怎么做?
脚本如下所示(删除不必要的内容):
send "show running-config interface $intf\r"
log_user 0
expect "#"
if {[string match "*service-policy input Access-Port*" $expect_out(buffer)]} {
set servicepolicy "yes"
} else {
set servicepolicy "no"
}
if {[string match "*mls qos trust dscp*" $expect_out(buffer)]} {
set mlsqos "yes"
} else {
set mlsqos "no"
}
if {[string matc "*Description*" $expect_out(buffer)]} {
EXTRACT DESCRIPTION STRING FROM $expect_out(buffer)
}
$expect_out(buffer) 的输出通常如下所示:
Current configuration : 559 bytes
!
interface GigabitEthernet1/0/17
description blablabla
switchport mode access
switchport voice vlan xxxxx
no logging event link-status
authentication event fail retry 0 action authorize vlan xxxxx
authentication event no-response action authorize vlan xxxxx
authentication host-mode multi-domain
authentication port-control auto
authentication violation restrict
mab
no snmp trap link-status
dot1x pae authenticator
dot1x timeout tx-period 5
dot1x timeout supp-timeout 10
no mdix auto
spanning-tree portfast
service-policy input Access-Port
end
"EXTRACT DESCRIPTION STRING FROM $expect_out(buffer)" 行是我想弄清楚的部分。我知道如何拆分行以仅获取描述,但我只是不知道如何从缓冲区变量中提取行本身。
使用带有 -line
选项的 regexp
命令:
% regexp -line {^\s*description (.*)$} $expect(buffer) -> desc
1
% puts $desc
blablabla
我假设描述不是多行的。
此外,如果您只需要一个布尔值,
set servicepolicy [string match "*service-policy input Access-Port*" $expect_out(buffer)]
或者,这样做
set servicepolicy [expr {[string match "*service-policy input Access-Port*" $expect_out(buffer)] ? "yes" : "no"}]
我正在编写一个连接到交换机然后显示接口配置的 expect 脚本。然后我分析这个输出来检查某些事情。我想存储我正在检查的其中一件事情的输出,我试图通过搜索 $expect_out(buffer) 来完成,尽管我很难找到如何做这个。
我应该怎么做?
脚本如下所示(删除不必要的内容):
send "show running-config interface $intf\r"
log_user 0
expect "#"
if {[string match "*service-policy input Access-Port*" $expect_out(buffer)]} {
set servicepolicy "yes"
} else {
set servicepolicy "no"
}
if {[string match "*mls qos trust dscp*" $expect_out(buffer)]} {
set mlsqos "yes"
} else {
set mlsqos "no"
}
if {[string matc "*Description*" $expect_out(buffer)]} {
EXTRACT DESCRIPTION STRING FROM $expect_out(buffer)
}
$expect_out(buffer) 的输出通常如下所示:
Current configuration : 559 bytes
!
interface GigabitEthernet1/0/17
description blablabla
switchport mode access
switchport voice vlan xxxxx
no logging event link-status
authentication event fail retry 0 action authorize vlan xxxxx
authentication event no-response action authorize vlan xxxxx
authentication host-mode multi-domain
authentication port-control auto
authentication violation restrict
mab
no snmp trap link-status
dot1x pae authenticator
dot1x timeout tx-period 5
dot1x timeout supp-timeout 10
no mdix auto
spanning-tree portfast
service-policy input Access-Port
end
"EXTRACT DESCRIPTION STRING FROM $expect_out(buffer)" 行是我想弄清楚的部分。我知道如何拆分行以仅获取描述,但我只是不知道如何从缓冲区变量中提取行本身。
使用带有 -line
选项的 regexp
命令:
% regexp -line {^\s*description (.*)$} $expect(buffer) -> desc
1
% puts $desc
blablabla
我假设描述不是多行的。
此外,如果您只需要一个布尔值,
set servicepolicy [string match "*service-policy input Access-Port*" $expect_out(buffer)]
或者,这样做
set servicepolicy [expr {[string match "*service-policy input Access-Port*" $expect_out(buffer)] ? "yes" : "no"}]