如何搜索Expect变量

时间:2015-08-29 03:25:21

标签: tcl expect

我正在处理连接到交换机的expect脚本,然后显示接口的配置。然后,我分析此输出以检查某些事情。我想存储我正在检查的其中一项内容的输出,我试图通过搜索$ expect_out(缓冲区)来完成,尽管我很难找到这该怎么做。

我应该怎么做呢?

脚本如下所示(删除不必要的东西):

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(缓冲区)的输出通常如下所示:

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

"提取描述STRING来自$ expect_out(缓冲区)" line是我想弄清楚的部分。我知道如何分割线以获取描述,但我只是不知道如何从缓冲区变量中提取线本身。

1 个答案:

答案 0 :(得分:2)

regexp命令与-line选项一起使用:

% 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"}]