Created
March 12, 2015 00:21
-
-
Save antoineMoPa/2c5e9dbb00141b732511 to your computer and use it in GitHub Desktop.
Count the number of times every IP was found in apache log files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Counts the number of times an IP was found in all the .log files in the folder | |
# first argument : log file name | |
# second argument : minimum number of error to print an ip | |
import sys | |
import os | |
import re | |
def extract_ips(): | |
os.system('echo "" > all.data') | |
os.system('find . | grep ".log" | xargs -I {} bash -c "cat {} >> all.data"') | |
f = open("all.data",'r') | |
r = re.compile('(\d+\.){3}\d+') | |
for line in f: | |
m = r.search(line) | |
if(m != None): | |
ip = m.group() | |
track_ip(ip) | |
print_numbers() | |
ips = {} | |
def track_ip(ip): | |
ip = str(ip) | |
if not ip in ips: | |
ips[ip] = 0 | |
ips[ip] = ips[ip] + 1 | |
def print_numbers(): | |
min = 0 | |
if(len(sys.argv) >= 2): | |
min = int(sys.argv[1]) | |
print(" --------------------------------------------------------------------") | |
print(" - showing all ips that have been found at least "+str(min)+" times -") | |
print(" --------------------------------------------------------------------") | |
for ip in ips: | |
if(ips[ip] >= min): | |
spaces = ''.join([' ' for i in range(0,20-len(ip))]) | |
print(ip + spaces + " : " + str(ips[ip])) | |
extract_ips() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment