我有很多像下面这样的文件,我需要根据不是在同一条线上的标准做一个简单的输出。
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
!
...这个包需要在“接口”之间,直到"!“。在这个包中,如果我有“身份验证端口-控制自动”行,输出将是:
FastEthernet0/8,WORKSTATION,DOT1X-OK如果没有,将是:
FastEthernet0/9,*** SERVER ***,DOT1X-NOK如果我没有带有"description“和”身份验证端口-控制自动“的行,比如包”接口FastEthernet0 0/7“,输出将是:
FastEthernet0/7,null,DOT1X-NOK我尝试了很多东西,但我的尝试都没有用。有什么办法用tcl制造这个东西吗?
发布于 2016-06-29 04:11:04
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发布于 2016-06-29 06:29:31
如果数据文件可以重写为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 unknown过程隐藏了。只要在file.txt文件中没有任何已经有Tcl定义的关键字,我们现在就可以将其作为源文件执行。如果愿意的话,以后只需编写新的命令过程来识别更多的关键字,就很容易扩展这一点。
文档:情商操作符、全局、如果、加入、伊塞特、流程、看跌期权、返回、设置、来源、未知
https://stackoverflow.com/questions/38089971
复制相似问题