首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用tcl根据不在同一条线上的标准进行简单输出

使用tcl根据不在同一条线上的标准进行简单输出
EN

Stack Overflow用户
提问于 2016-06-29 03:38:23
回答 2查看 84关注 0票数 0

我有很多像下面这样的文件,我需要根据不是在同一条线上的标准做一个简单的输出。

代码语言:javascript
复制
file.txt

...
interface FastEthernet0/7 
switchport access vlan 10
switchport mode access
switchport nonegotiate
speed 100
duplex full
srr-queue bandwidth share 10 10 60 20
priority-queue out 
mls qos trust cos
snmp trap mac-notification change added
snmp trap mac-notification change removed
auto qos voip trust 
spanning-tree portfast
!
interface FastEthernet0/8
description WORKSTATION
switchport access vlan 20
switchport mode access
switchport nonegotiate
switchport voice vlan 10
srr-queue bandwidth share 10 10 60 20
priority-queue out 
authentication event fail action next-method
authentication event server dead action reinitialize vlan 20
authentication event server dead action authorize voice
authentication event server alive action reinitialize 
authentication host-mode multi-domain
authentication order dot1x mab
authentication violation restrict
authentication port-control auto
mab
mls qos trust device cisco-phone
mls qos trust cos
snmp trap mac-notification change added
snmp trap mac-notification change removed
dot1x pae authenticator
dot1x timeout tx-period 5
auto qos voip cisco-phone 
spanning-tree portfast
service-policy input AutoQoS-Police-CiscoPhone
ip dhcp snooping limit rate 10
ip dhcp snooping trust
!
interface FastEthernet0/9
description *** SERVER ***
switchport access vlan 20
switchport mode access
switchport nonegotiate
switchport voice vlan 10
srr-queue bandwidth share 10 10 60 20
priority-queue out 
mls qos trust device cisco-phone
mls qos trust cos
snmp trap mac-notification change added
snmp trap mac-notification change removed
auto qos voip cisco-phone 
spanning-tree portfast
service-policy input AutoQoS-Police-CiscoPhone
ip dhcp snooping limit rate 10
ip dhcp snooping trust
!
...

这个包需要在“接口”之间,直到"!“。在这个包中,如果我有“身份验证端口-控制自动”行,输出将是:

代码语言:javascript
复制
FastEthernet0/8,WORKSTATION,DOT1X-OK

如果没有,将是:

代码语言:javascript
复制
FastEthernet0/9,*** SERVER ***,DOT1X-NOK

如果我没有带有"description“和”身份验证端口-控制自动“的行,比如包”接口FastEthernet0 0/7“,输出将是:

代码语言:javascript
复制
FastEthernet0/7,null,DOT1X-NOK

我尝试了很多东西,但我的尝试都没有用。有什么办法用tcl制造这个东西吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-29 04:11:04

代码语言:javascript
复制
set interfaces [regexp -all -inline {interface.*?!} $input]
foreach interface $interfaces {
    set slot_info [regexp -line -inline {interface\s+(.*)} $interface]
    if {[llength $slot_info]==2} {
        set slot [lindex $slot_info 1]
    } else {
        set slot NA
    }
    set desc_info [regexp -line -inline {description\s+(.*)} $interface]
    if {[llength $desc_info]==2} {
        set desc [lindex $desc_info 1]
    } else {
        set desc null
    }
    if {[string first "authentication port-control auto" $interface]!=-1} {
        set dot1x "DOT1X-OK"
    } else {
        set dot1x "DOT1X-NOK"
    }
    puts "$slot,$desc,$dot1x"
}

输出:

代码语言:javascript
复制
FastEthernet0/7 ,null,DOT1X-NOK
FastEthernet0/8,WORKSTATION,DOT1X-OK
FastEthernet0/9,*** SERVER ***,DOT1X-NOK
票数 0
EN

Stack Overflow用户

发布于 2016-06-29 06:29:31

如果数据文件可以重写为Tcl脚本,那么这样做通常是有用的。

代码语言:javascript
复制
set data {{} null DOT1X-NOK}

proc interface slot {
    global data
    lset data 0 $slot
}

proc authentication args {
    global data
    if {[join $args] eq "port-control auto"} {
        lset data 2 DOT1X-OK
    }
}

proc description args {
    global data
    lset data 1 [join $args]
}

proc ! {} {
    global data
    puts [join $data ,]
    set data {{} null DOT1X-NOK}
    return
}

proc unknown args {}

source file.txt

以我们感兴趣的关键字开头的行将得到相应的命令过程,该过程存储行上的“参数”。那些我们不感兴趣的行/关键字被no unknown过程隐藏了。只要在file.txt文件中没有任何已经有Tcl定义的关键字,我们现在就可以将其作为源文件执行。如果愿意的话,以后只需编写新的命令过程来识别更多的关键字,就很容易扩展这一点。

文档:情商操作符、全局如果加入伊塞特流程看跌期权返回设置来源未知

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38089971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档