使用python匹配条目

时间:2013-03-12 10:33:12

标签: python parsing correlation

我已解析了两个日志,因此只有CVE编号写入文本文件,因此它们显示为一个列表。这是输出的一部分;

NeXpose Results: CVE-2007-6519
NeXpose Results: CVE-1999-0559
NeXpose Results: CVE-1999-1382
Snort results: CVE-1999-0016
Snort results: CVE-1999-0016
Snort results: CVE-1999-0016
Snort results: CVE-1999-0016
Snort results: CVE-1999-0016

这就像文件一样。现在我想让我的代码加密CVE数字并查看是否有任何NeXpose CVE与snort CVE匹配,因为我正在考虑将两者相关联。这是我的代码;

#!/usr/bin/env python
nexpose = {}
snort = {}

CVE = open("CVE.txt","r")
cveWarning = open("Warning","w")

for line in CVE.readlines():
        list_of_line = line.split(' ')

    if "NeXpose" in list_of_line[0]:
        nexResults = list_of_line[2]
        #print 'NeXpose: ', nexResults



    if "Snort" in list_of_line[0]:
        cveResults = list_of_line[2]
        #print 'Snort: ', cveResults


a_match = [True for match in nexResults if match in cveResults]
print a_match

如果有更好的方法,请告诉我,因为我认为我可能会使事情过于复杂。

2 个答案:

答案 0 :(得分:4)

您是否考虑过Python集?

#!/usr/bin/python

lines = open('CVE.txt').readlines()
nexpose = set([l.split(':')[1].strip() for l in lines if l.startswith('NeXpose')])
snort   = set([l.split(':')[1].strip() for l in lines if l.startswith('Snort')])

# print 'Nexpose: ', ', '.join(nexpose)
# print 'Snort  : ', ', '.join(snort)

print 'CVEs both in Nexpose and Snort  : ', ', '.join(snort.intersection(nexpose))

答案 1 :(得分:0)

我建议使用python集。

nex = set()
sno = set()
for line in CVE.readlines():
    list_of_line = line.split(' ')

    if(list_of_line[0]=="NeXpose"):
        nex.add(list_of_line[2])
    if(list_of_line[0]=="Snort"):
        sno.add(list_of_line[2])

inboth = sno.intersection(nex)