【发布时间】:2016-01-26 15:51:21
【问题描述】:
我有一个包含内容的文件。
file.txt
interface FastEthernet0/20
snmp trap mac-notification change added
interface FastEthernet0/21
snmp trap mac-notification change added
snmp trap mac-notification change removed
interface FastEthernet0/22
snmp trap mac-notification change removed
interface FastEthernet0/23
snmp trap mac-notification change added
snmp trap mac-notification change removed
interface FastEthernet0/24
snmp trap mac-notification change added
snmp trap mac-notification change removed
interface GigabitEthernet0/1
interface GigabitEthernet0/2
我做了一个脚本来读取这个文件,如果界面没有“snmp trap mac-notification change added”和“snmp trap mac-notification change removed”这两行,我需要把这两个命令分别位于界面下方。
期望输出:
conf t
interface FastEthernet0/20
snmp trap mac-notification change added
snmp trap mac-notification change removed
conf t
interface FastEthernet0/21
snmp trap mac-notification change added
snmp trap mac-notification change removed
conf t
interface FastEthernet0/22
snmp trap mac-notification change added
snmp trap mac-notification change removed
conf t
interface GigabitEthernet0/1
snmp trap mac-notification change added
snmp trap mac-notification change removed
conf t
interface GigabitEthernet0/2
snmp trap mac-notification change added
snmp trap mac-notification change removed
脚本有两个问题。一种是如果接口只有“snmp trap mac-notification change removed”行,脚本会跳过该块并转到下一个接口,直到找到具有三个命令(接口,添加snmp和删除snmp)的块或当不有两个 snmp 命令。另一个问题是,当最后一行“interface GigabitEthernet0/2”没有 snmp 命令时,脚本也会跳过它。
这里是脚本。
set intf {}
set PR {}
set PPR {}
set PPRfullintf {}
set PPRintf {}
set PRfullintf {}
set PRintf {}
set 4 {}
set P4 {}
set f [open "file.txt" r]
foreach a [split [read -nonewline $f] \n] {
set 1 [lindex $a 1]
set 0 [lindex $a 0 ]
set 4 [lindex $a 4 ]
if { [regexp {^Fa|^Gi} $1] } { set intf1 "interface $1" }
if { $4 != "added" && $4 != "removed" && $P4 == "added" && $PR == "interface" } {
puts "conf t\r"
puts "$PRfullintf\r"
puts "snmp trap mac-notification change added\r"
puts "snmp trap mac-notification change removed\r"
}
if { $0 == "interface" && $PR == "interface" } {
puts "conf t\r"
puts "$PRfullintf\r"
puts "snmp trap mac-notification change added\r"
puts "snmp trap mac-notification change removed\r"
}
set P4 $4
if { $4 == "added" || $4 == "removed" } { set P4 $4 }
set PR $0
set PRfullintf $intf1
set PRintf $1
}
close $f
意外输出:
conf t
interface FastEthernet0/20
snmp trap mac-notification change added
snmp trap mac-notification change removed
conf t
interface FastEthernet0/21
snmp trap mac-notification change added
snmp trap mac-notification change removed
conf t
interface GigabitEthernet0/1
snmp trap mac-notification change added
snmp trap mac-notification change removed
我认为尝试使用“foreach”命令来实现这一点是不合适的,但我没有找到另一种方法来做到这一点。如何修复所有错误?
谢谢。
【问题讨论】:
标签: tcl