2012-03-12 53 views
1

以下是我正在嘗試編寫的腳本的一部分。該腳本打開我的iptables日誌,日誌中的每行包含下面示例中的詳細信息。Python - 列出問題(多個列表?)

 
#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 '======================================================' 

我試圖讓腳本只打印我想要的物品,但是當由腳本輸出它看起來像這樣,這似乎是一個列表。我希望它在沒有[]'的情況下打印。在網上看起來好像每個變量(time,mac,src等)都是一個列表。我不知道如何合併它們。我見過參考加入,但不知道如何使用它的這個例子。有人可以協助嗎?

 
['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'] 
+2

你爲什麼不用一個正則表達式來做這件事? – Kimvais 2012-03-12 10:50:12

回答

1

爲什麼不只是解壓縮列表?

>>> time = [0] 
>>> [time] = time 
>>> time 
0 
+0

這樣做......我在那裏做了什麼?謝謝 – 2012-03-12 10:56:57

+0

您已解開列表。 https://www.google.ca/search?q=list+unpacking+python – 2012-03-12 11:47:00

+0

列表解壓工程如tuple解包: >>> tuple =(0,1) >>>(x,y)=元組 >>> x >>> y 元組解包是更多使用的方式(因爲您經常從具有多個結果的函數/方法返回元組)。 – QuidNovi 2012-03-12 14:25:27

0

你可能只是做:

foo = re.findall(…)[0] 

,如果你希望只有一個結果,對於任何re.findall

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' 

(源= http://docs.python.org/library/re.html

+0

良好的通話 - 謝謝! – 2012-03-12 11:01:20

+0

['re.search'](http://docs.python.org/library/re.html#re.search)如果字符串中沒有位置與模式匹配,則返回'None'。所以,在訪問'MatchObject'之前,你應該檢查你的結果是不是'None'。 – 2012-03-12 11:06:20

0

re.findall返回匹配的列表。在你的情況下,你只有一個值的列表。如果情況總是如此,那麼@ x539答案將獲得列表中的第一項。

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'