以下是我正在尝试编写的脚本的一部分。该脚本打开我的iptables日志,日志中的每一行都包含以下示例中的详细信息。
#example of a single line #Mar 9 14:57:51 machine kernel: [23780.638839] IPTABLES Denied UDP: IN=p21p1 OUT= MAC=ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00 SRC=10.100.1.4 DST=10.100.1.63 LEN=78 TOS=0x00 PREC=0x00 TTL=128 ID=10898 PROTO=UDP$ # Read file in a line at a time for line in iptables_log.readlines(): #find time based on 4 letters, 2 spaces, up to 2 numbers, 1 space, then standard 10:10:10 time format time = re.findall('(^\w{1,4}\s\s\d{1,2}\s\d\d:\d\d:\d\d)', line) #mac lookup mac = re.findall('MAC=(?:\w\w:\w\w:\w\w:\w\w\:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)', line) #source port src = re.findall('SRC=(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', line) #destination port dst = re.findall('DST=(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', line) #protocol proto = re.findall('PROTO=(?:\w{3,4})', line) #sourceport sourceport = re.findall('SPT=(?:\w{1,5})', line) #destport destport = re.findall('DPT=(?:\w{1,5})', line) print time, mac, src, dst, proto, sourceport, destport print '======================================================'
我正在尝试让脚本只打印我想要的项目,但是当脚本输出时它看起来像这样,这似乎是一个列表。我希望它在没有[]''的情况下打印。在线查看似乎每个变量(时间,mac,src等)本身就是一个列表。我不确定如何将它们结合起来。我已经看到了对join的引用,但我不确定如何使用它这个例子。有人可以协助吗?
['Mar 9 14:57:51'] ['MAC=ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00'] ['SRC=10.100.1.4'] ['DST=10.100.1.63'] ['PROTO=UDP'] ['SPT=137'] ['DPT=137']
答案 0 :(得分:3)
re.findall返回一个列表
def findall(pattern, string, flags=0):
"""Return a list of all non-overlapping matches in the string.
If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result."""
return _compile(pattern, flags).findall(string)
我会改用re.search。
>>> import re
>>> st = '''Mar 9 14:57:51 machine kernel: [23780.638839] IPTABLES Denied UDP: IN=p21p1 OUT= MAC=ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00 SRC=10.100.1.4 DST=10.100.1.63 LEN=78 TOS=0x00 PREC=0x00 TTL=128 ID=10898 PROTO=UDP$'''
>>> x = re.search('(^\w{1,4}\s\s\d{1,2}\s\d\d:\d\d:\d\d)',st)
>>> x.group(0)
'Mar 9 14:57:51'
答案 1 :(得分:1)
为什么不打开清单?
>>> time = [0]
>>> [time] = time
>>> time
0
答案 2 :(得分:0)
你可以这样做:
foo = re.findall(…)[0]
如果你只期望一个结果,对于任何re.findall
答案 3 :(得分:0)
re.findall返回匹配列表。在您的情况下,您获得的列表只有一个值。如果总是如此,则@ x539回答将获得列表中的第一项。
答案 4 :(得分:0)
我建议使用单个正则表达式作为整行,使用命名组,例如:
>>> r = re.compile(r'^(?P<date>\w{1,4}\s\d{1,2}\s\d\d\:\d\d\:\d\d) (?P<hostname>\w+) kernel: (\[[0-9]+.[0-9]+\]) IN=(?P<ifacein>[a-z0-9]*) OUT=(?P<ifaceout>[a-z0-9]*) MAC=(?P<mac>[a-f0-9:]+) SRC=(?P<src>[\w.:]+) DST=(?P<dst>[\w:.]+) LEN=(?P<len>[0-9]+) TOS=0x(?P<tos>[0-9a-f]+) PREC=0x(?P<prec>[0-9a-f]+) TTL=(?P<ttl>[0-9]+) ID=(?P<id>[0-9]+) PROTO=(?P<proto>[A-Z]+) SPT=(?P<spt>[0-9]+) DPT=(?P<dpt>[0-9]+) LEN=(?P<len2>[0-9]+)')
>>> d = r.match(line).groupdict()
>>> d['dst']
'255.255.255.255'
>>> d['proto']
'UDP'
>>> d['dpt']
'17500'
您也可以轻松地将所有内容全部包含在包含所需字段的单个字符串中:
>>> ' '.join([d[_] for _ in ("date", "mac", "src", "dst", "proto", "spt", "dpt")])
'Mar 12 13:06:10 ff:ff:ff:ff:ff:ff:00:18:8b:da:86:37:08:00 192.168.0.221 255.255.255.255 UDP 17500 17500'