使用python匹配条目

我已经解析了两个日志,以便仅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

如果有更好的方法可以进行此操作,请告诉我,因为我认为事情可能太复杂了.

解决方法:

您是否考虑过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))
上一篇:Linux运行shell脚本提示No such file or directory错误的解决办法


下一篇:Interpolation, Lines(插值、线性插值)