基于使用tcl不在同一行上的标准进行简单输出

时间:2016-06-29 03:38:23

标签: tcl

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

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
!
...

捆绑包需要介于"界面"直到"!"。在这个捆绑包内,如果我有#34;身份验证端口控制自动",输出将是:

FastEthernet0/8,WORKSTATION,DOT1X-OK

如果没有,将是:

FastEthernet0/9,*** SERVER ***,DOT1X-NOK

如果我没有"描述"和"认证端口控制auto" ,就像捆绑"接口FastEthernet0 / 7",输出将是:

FastEthernet0/7,null,DOT1X-NOK

我尝试了很多东西,但我的尝试都没有。有没有想过用tcl制作这个东西?

2 个答案:

答案 0 :(得分:1)

如果数据文件可以作为Tcl脚本重新创建,那么这样做通常很有用。

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-op unknown过程隐藏。只要在file.txt文件中没有任何关键字已经有Tcl定义,我们就可以将其作为源文件执行。如果需要,稍后可以通过编写新的命令过程来识别更多关键字来轻松扩展它。

文档:eq运营商,globalifjoinlsetprocputs,{{ 3}},returnsetsource

答案 1 :(得分:0)

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

输出

FastEthernet0/7 ,null,DOT1X-NOK
FastEthernet0/8,WORKSTATION,DOT1X-OK
FastEthernet0/9,*** SERVER ***,DOT1X-NOK