Python log parsing

From Cryptolife

Jump to: navigation, search



Extracting ip addresses from a remote txt file

#!/usr/bin/env python
import urllib2
import re

url = 'http://www.emergingthreats.org/fwrules/emerging-Block-IPs.txt'
http = urllib2.urlopen(url)
iploca = http.read()

ip = re.findall('(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', iploca)

for c in ip: print c

save in some file called extract.py an run it with ./extract.py

  1. ./extract.py
    98.136.92.79
    98.143.147.252
    98.143.159.138
    99.227.64.39
    99.233.248.19


Extracting ip addresses from a local log file.

#!/usr/bin/env python
import urllib2
import re
import fileinput
for line in fileinput.input(['/var/log/apache2/access.log']):

        ip = re.findall('(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', line)
        for c in ip: print c


save in some file called extract.py an run it with ./extract.py

  1. ./extract.py
    1.9.1.5
    1.8.1.23
    2.0.0.23
    1.9.1.5
    1.9.1.5
    1.9.1.5






Personal tools
Creative Commons License