Question

The below is a part of a script i'm trying to write. The script opens my iptables log, each line in the log contains the details in the example below.

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

I'm trying to get the script to print only the items i want, but when its output by the script it looks like this, which would seem to be a list. I want it to print without the [] ''. Looking online it seems like every variable (time, mac, src, etc) are a list themselves. I'm not sure how to combine them. I have seen reference to join but am not sure how to use it this example. Can someone assist please?

['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']
Was it helpful?

Solution

Why not just unpack your lists ?

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

OTHER TIPS

re.findall returns a list

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)

I would use re.search instead.

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

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

You could just do:

foo = re.findall(…)[0]

if you expect only one result, for any re.findall

re.findall returns a list of matches. In your case you are getting lists with only one value. If that is always the case then @x539 answer will get the first item in the list.

I suggest using a single regexp for whole line, with named groups such as:

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

You can also easily get it all back in a single string with fields you want:

>>> ' '.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'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top