我有一个带有主案例和嵌套案例的 ascii 纯文本文件输入文件,如下所示:我想比较以'$'开头的实例之间的细节和@ExtendedAttr = nvp_add 函数在下面的输入文件中,每个案例在 switch($specific-陷阱),但是当我在 python 脚本部分下运行脚本时,所有嵌套的情况也会被打印出来,我不希望嵌套的情况在这里被打印出来,并且脚本只考虑 switch($specific-case) 下的情况。我该怎么做这个帮助!:
Input file:
************
case ".1.3.6.1.4.1.27091.2.9": ### - Notifications from JNPR-TIMING-MIB (1105260000Z)
log(DEBUG, "<<<<< Entering... juniper-JNPR-TIMING-MIB.include.snmptrap.rules
>>>>>")
@Agent = "JNPR-TIMING-MIB"
@Class = "40200"
$OPTION_TypeFieldUsage = "3.6"
switch($specific-trap)
{
case "1": ### trapMsgNtpStratumChange
##########
# $1 = trapAttrSource
# $2 = trapAttrSeverity
##########
$trapAttrSource = $1
$trapAttrSeverity = lookup($2, TrapAttrSeverity)
$OS_EventId = "SNMPTRAP-juniper-JNPR-TIMING-MIB-trapMsgNtpStratumChange"
@AlertGroup = "NTP Stratum Status"
@AlertKey = "Source: " + $trapAttrSource
@Summary = "NTP Stratum Changes" + " ( " + @AlertKey + " ) "
switch($2)
{
case "1":### clear
$SEV_KEY = $OS_EventId + "_clear"
@Summary = "End of: " + @Summary
$DEFAULT_Severity = 1
$DEFAULT_Type = 2
$DEFAULT_ExpireTime = 0
case "2":### none
$SEV_KEY = $OS_EventId + "_none"
$DEFAULT_Severity = 2
$DEFAULT_Type = 1
$DEFAULT_ExpireTime = 0
case "3":### minor
$SEV_KEY = $OS_EventId + "_minor"
$DEFAULT_Severity = 3
$DEFAULT_Type = 1
$DEFAULT_ExpireTime = 0
case "4":### major
$SEV_KEY = $OS_EventId + "_major"
$DEFAULT_Severity = 4
$DEFAULT_Type = 1
$DEFAULT_ExpireTime = 0
case "5":### critical
$SEV_KEY = $OS_EventId + "_critical"
$DEFAULT_Severity = 5
$DEFAULT_Type = 1
$DEFAULT_ExpireTime = 0
default:
$SEV_KEY = $OS_EventId + "_unknown"
$DEFAULT_Severity = 2
$DEFAULT_Type = 1
$DEFAULT_ExpireTime = 0
}
update(@Severity)
$trapAttrSeverity = $trapAttrSeverity + " ( " + $2 + " )"
@Identifier = @Node + " " + @AlertKey + " " + @AlertGroup + " " +
$DEFAULT_Type + " " + @Agent + " " + @Manager + " " + $specific-trap
if(match($OPTION_EnableDetails, "1") or match($OPTION_EnableDetails_juniper,
"1")) {
details($trapAttrSource,$trapAttrSeverity)
}
@ExtendedAttr = nvp_add(@ExtendedAttr, "trapAttrSource", $trapAttrSource,
"trapAttrSeverit")
case "2": ### trapMsgNtpLeapChange
##########
# $1 = trapAttrSource
# $2 = trapAttrSeverity
##########
$trapAttrSource = $1
$trapAttrSeverity = lookup($2, TrapAttrSeverity)
$OS_EventId = "SNMPTRAP-juniper-JNPR-TIMING-MIB-trapMsgNtpLeapChange"
@AlertGroup = "NTP Leap Status"
@AlertKey = "Source: " + $trapAttrSource
@Summary = "NTP Leap Changes" + " ( " + @AlertKey + " ) "
switch($2)
{
case "1":### clear
$SEV_KEY = $OS_EventId + "_clear"
@Summary = "End of: " + @Summary
$DEFAULT_Severity = 1
$DEFAULT_Type = 2
$DEFAULT_ExpireTime = 0
case "2":### none
$SEV_KEY = $OS_EventId + "_none"
$DEFAULT_Severity = 2
$DEFAULT_Type = 1
$DEFAULT_ExpireTime = 0
case "3":### minor
$SEV_KEY = $OS_EventId + "_minor"
$DEFAULT_Severity = 3
$DEFAULT_Type = 1
$DEFAULT_ExpireTime = 0
case "4":### major
$SEV_KEY = $OS_EventId + "_major"
$DEFAULT_Severity = 4
$DEFAULT_Type = 1
$DEFAULT_ExpireTime = 0
case "5":### critical
$SEV_KEY = $OS_EventId + "_critical"
$DEFAULT_Severity = 5
$DEFAULT_Type = 1
$DEFAULT_ExpireTime = 0
default:
$SEV_KEY = $OS_EventId + "_unknown"
$DEFAULT_Severity = 2
$DEFAULT_Type = 1
$DEFAULT_ExpireTime = 0
}
update(@Severity)
$trapAttrSeverity = $trapAttrSeverity + " ( " + $2 + " )"
@Identifier = @Node + " " + @AlertKey + " " + @AlertGroup + " " +
$DEFAULT_Type + " " + @Agent + " " + @Manager + " " + $specific-trap
if(match($OPTION_EnableDetails, "1") or match($OPTION_EnableDetails_juniper,
"1")) {
details($trapAttrSource,$trapAttrSeverity)
}
@ExtendedAttr = nvp_add(@ExtendedAttr, "trapAttrSource", $trapAttrSource,
"trapAttrSeverity", $trapAttrSeverity)
Below is the code which I use suggested by Vaibhav Aggarwal one of the member in
this stakeoverflow.
Python Script
**************
import re
`caselines_index = []
cases = []
readlines = []
def read(in_file):
global cases
global caselines_index
global readlines
with open(in_file, 'r') as file:
for line in file.readlines():
readlines.append(line.strip())
for line in readlines:
case_search = re.search("case\s\".+?\"\:\s", line)
if case_search:
caselines_index.append(readlines.index(line))
#print caselines_index
caselines_index_iter = iter(caselines_index)
int_line_index = int(next(caselines_index_iter))
int_next_index = int(next(caselines_index_iter))
while True:
try:
case_text = ' '.join(readlines[int_line_index:int_next_index]).strip()
case = [readlines[int_line_index].strip(), case_text]
cases.append(case)
int_line_index = int_next_index
int_next_index = int(next(caselines_index_iter))
except StopIteration:
case_text = ' '.join(readlines[int_line_index:len(readlines) - 1]).strip()
case = [readlines[int_line_index].strip(), case_text]
cases.append(case)
break
def work():
MATCH = 1
for case_list in cases:
details = []
nvp_add = []
caseline = case_list[0].strip()
nvp = re.findall("details\(.+?\)", case_list[1].strip())
for item in nvp:
result_list = re.findall("(\$.+?)[\,\)]", item)
for result in result_list:
if "$*" not in result:
details.append(result)
nvp = re.findall("nvp_add\(.+?\)", case_list[1].strip())
for item in nvp:
result_list = re.findall("(\$.+?)[\,\)]", item)
for result in result_list:
if "$*" not in result:
nvp_add.append(result)
missing_from_details, missing_from_nvp_add = [], []
missing_from_details = [o for o in nvp_add if o not in set(details)]
missing_from_nvp_add = [o for o in details if o not in set(nvp_add)]
if missing_from_nvp_add or missing_from_details:
MATCH = 0
print caseline + " LINE - " + str(readlines.index(caseline) + 1)
for mismatch in missing_from_details:
print "Missing from details:"
print mismatch
for mismatch in missing_from_nvp_add:
print "Missing from nvp_add:"
print mismatch
print "\n"
if MATCH == 1:
print "MATCH"
else:
print "MISMATCHES"
def main():
in_file = "C:/target1.txt"
read(in_file)
work()
if __name__=="__main__":
main()