在文件(下面的示例数据)中,有一些类似输出的组,我们在其中搜索一行中的特定文本。在每组文字中,如果issuerID
和subjectID
都存在,则打印这两个项目以及sourceIP
和destIP
。如果没有,请跳过并继续下一步。下面的代码有效,但我希望添加逻辑,以便在issuerID
和subjectID
匹配时仅打印所需的信息。
这是脚本的当前迭代。
#!/usr/bin/python
import re
sourceIP = 'Source IP:'
destIP = 'Destination IP:'
issuerID = 'Issuer ID:'
subjectID = 'Subject ID:'
for line in open('data.txt', 'r'):
line = line.strip()
if line.startswith(sourceIP):
sourceIPline = line
elif line.startswith(destIP):
destIPline = line
elif line and line.startswith(issuerID):
issuerDomain = re.search("www[\w.com]+", line)
elif line and line.startswith(subjectID):
subjectDomain = re.search("www[\w.net]+", line)
print (sourceIPline)
print (destIPline)
print issuerDomain.group()
print subjectDomain.group()
print
如果我运行的数据集下面的值不符合条件,我会收到错误。我希望这个小组被忽略并继续前进。
$ ./dparser.py
Source IP: 10.0.0.126
Destination IP: 8.8.8.8
www.domain.com
www.domain.net
Source IP: 10.0.0.126
Destination IP: 8.8.4.4
www.domain2.com
Traceback (most recent call last):
File "./dparser.py", line 22, in <module>
print subjectDomain.group()
AttributeError: 'NoneType' object has no attribute 'group'
以下是包含两个组的示例数据集。
-------------------------------
Template ID is 45841
Application Label: 443
Source IP: 10.0.0.126
Destination IP: 8.8.8.8
Source Port: 38946
Dest Port: 443
flowStartTime: 2013-12-30 20:20:21
flowEndTime: 2013-12-30 20:21:37
flowEndReason: 3
Protocol: 6
Octet Total Count: 8497
Rev Octet count: 28802
Packet Total Count: 30
Rev Packet Total Count: 32
TCP Sequence Number: 561150853
Initial TCP Flags: S
Union TCP Flags: APF
Reverse TCP Sequence Number: 4118697381
Reverse Initial TCP Flags: AS
Reverse Union TCP Flags: APF
SSL Server Cipher: 49171
-- X.509 Cert 0 (v.2) ---
Issuer ID: 3 Val: www.domain.com
Subject ID: 3 Val: www.domain.net
Valid Not Before: 131015000000Z
Not Valid After: 140211235959Z
Public Key Length: 0
--- End of Cert 0 ---
-------------------------------
Template ID is 45841
Application Label: 443
Source IP: 10.0.0.126
Destination IP: 8.8.4.4
Source Port: 42529
Dest Port: 443
flowStartTime: 2013-12-30 20:20:22
flowEndTime: 2013-12-30 20:21:37
flowEndReason: 3
Protocol: 6
Octet Total Count: 8341
Rev Octet count: 26678
Packet Total Count: 27
Rev Packet Total Count: 30
TCP Sequence Number: 3052048930
Initial TCP Flags: S
Union TCP Flags: APF
Reverse TCP Sequence Number: 101975511
Reverse Initial TCP Flags: AS
Reverse Union TCP Flags: APF
SSL Server Cipher: 49171
-- X.509 Cert 0 (v.2) ---
Issuer ID: 3 Val: www.domain2.com
Subject ID: 3 Val: Some Subject
Valid Not Before: 130727000000Z
Not Valid After: 140118000000Z
Public Key Length: 0
--- End of Cert 0 ---
答案 0 :(得分:1)
你需要验证你的正则表达式找到了什么。在这种情况下,第二个数据集没有域,而是说“Some Subject”。
你想要这样的东西:
if subjectDomain:
print subjectDomain.group()
else:
print line # whatever you want here...
此外,无论如何都没有正确定义你的正则表达式。 []符号构成一组要检查的东西。你真的想要r'www。\ w + .com'否则你允许'wwwww'和'www.w.w'。