Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse apache log that has 2 ip addresses

Tags:

python

apache

I have an apache log file I'm trying to parse. I've found a few different methods, including apachelog, the two answers here, and this. Using any of those methods I was able to parse most of the lines in my log. However, some lines have 2 ip addresses:

xxx.xx.xx.xxx, yy.yyy.yy.yyy - - [14/Feb/2013:03:55:21 +0000] "GET /alink HTTP/1.0" 200 90210 "http://www.google.com/search" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko; Google Web Preview) Chrome/22.0.1229 Safari/537.4"

None of the methods mentioned were able to parse this line correctly. (I even tried apachelog's virtualhost option). Any suggestions? I am using the latter method I mentioned (but am open to anything), e.g.:

parts = [
    r'(?P<host>\S+)',                   # host %h
    r'\S+',                             # indent %l (unused)
    r'(?P<user>\S+)',                   # user %u
    r'\[(?P<time>.+)\]',                # time %t
    r'"(?P<request>.+)"',               # request "%r"
    r'(?P<status>[0-9]+)',              # status %>s
    r'(?P<size>\S+)',                   # size %b (careful, can be '-')
    r'"(?P<referer>.*)"',               # referer "%{Referer}i"
    r'"(?P<agent>.*)"',                 # user agent "%{User-agent}i"
    ]
    pattern = re.compile(r'\s+'.join(parts)+r'\s*\Z')

    for line in open(log):
        try:    
            m = pattern.match(line)
            if m:
                res = m.groupdict()
                data.append(res)
            if not m:
                print line
        except:
            print line
like image 441
user_78361084 Avatar asked Nov 25 '25 19:11

user_78361084


1 Answers

You can modify the first component of the regular expression in your listing to allow for a comma-separated list of hosts. The following works for your example line:

import re
parts = [
    r'(?P<host>\S+(,\s*\S+)*)',         # comma-separated list of hosts                             
    r'\S+',                             # indent %l (unused)                
    r'(?P<user>\S+)',                   # user %u                           
    r'\[(?P<time>.+)\]',                # time %t                           
    r'"(?P<request>.+)"',               # request "%r"                      
    r'(?P<status>[0-9]+)',              # status %>s                        
    r'(?P<size>\S+)',                   # size %b (careful, can be '-')     
    r'"(?P<referer>.*)"',               # referer "%{Referer}i"             
    r'"(?P<agent>.*)"',                 # user agent "%{User-agent}i"       
]
pattern = re.compile(r'\s+'.join(parts)+r'\s*\Z')

test = 'xxx.xx.xx.xxx, yy.yyy.yy.yyy - - [14/Feb/2013:03:55:21 +0000] "GET /alink HTTP/1.0" 200 90210 "http://www.google.com/search" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML,like Gecko; Google Web Preview) Chrome/22.0.1229 Safari/537.4"'
m = pattern.match(test)
res = m.groupdict()

After the above commands, res['host'] contains xxx.xx.xx.xxx, yy.yyy.yy.yyy. If you need the host addresses separately, you can use res['host'].split(',') the get a list of addresses.

like image 196
jochen Avatar answered Nov 28 '25 15:11

jochen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!