-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogprocessor.py
125 lines (95 loc) · 2.54 KB
/
logprocessor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# /usr/bin/python
# Reads in botbase, compares to logfile and processes
# generates site bot report
from __future__ import generators
from sys import argv
import os, sys
import csv
import time
csv.field_size_limit(1000000000000000)
if len(sys.argv) != 3:
print 'Usage: logprocessor.py <bot list file> <log file>'
botBase = sys.argv[1]
logFile = sys.argv[2]
resultsFile = 'botscanresults.txt'
brokeFile = 'bustedlinks.txt'
b = open(botBase,'U')
bots = csv.reader(b)
bBase = {}
results = []
badLinks = []
ips = []
tip = []
# first, read in the botbase and build a list of agent names and ips - not the most efficient but it's portable
print "Building robots database..."
for row in bots:
bAgent = row[0]
bAgent = bAgent.strip()
bip = row[3]
bip = bip.replace('#','')
bip = bip.strip()
bBase[bip] = bAgent
print "Processing log file..."
l = open(logFile,'U')
counter = 0
x = l.read()
l.close()
for r in x:
if r.find(','):
deLim = ','
elif r.find('-'):
deLim = '-'
else:
delim = 'none'
counter = counter + 1
if counter > 2:
break
l = open(logFile,'U')
log = csv.reader(l, delimiter = ' ')
counter = 0
header = 'Search engine\tIP address\tDate visited\tURL visited\tMode\tStatus code\tBytes transferred\tAgent\n'
results.append(header)
for row in log:
counter = counter + 1
ip = row[0]
ip = ip.strip()
agent = row[9]
sCode = row[6]
bytes = row[7]
theDate = row[3]
theDate = theDate.replace('[','')
URL = row[5]
URL = URL.split(' ')
thisURL = URL[1]
method = URL[0]
if ip in bBase:
nextline = bBase[ip] + '\t' + ip + '\t' + theDate + '\t' + thisURL + '\t' + method + '\t' + sCode + '\t' + bytes + '\t' + agent
results.append(nextline)
l.close()
r = open(resultsFile,'a')
for rs in results:
rs = rs + "\n"
r.write(rs)
print "Done - now looking for 404s. Processed ", counter, " lines of log file data, by the way."
l = open(logFile,'U')
log = csv.reader(l, delimiter = ' ')
counter = 0
header = "URL\tResponse code\tDate crawled\tReferrer\n"
badLinks.append(header)
for row in log:
sCode = row[6]
if (sCode.find('200') == -1) & (sCode.find('301') & (sCode.find('304'))):
URL = row[5]
URL = URL.split(' ')
thisURL = URL[1]
method = URL[0]
theDate = row[3]
theDate = theDate.replace('[','')
referrer = row[8]
nextline = thisURL + "\t" + sCode + "\t" + theDate + "\t" + referrer + "\n"
badLinks.append(nextline)
l.close()
r = open(brokeFile,'a')
for rs in badLinks:
r.write(rs)
print "Done. Files were saved to wherever you put the logs. They're called botscanresults.txt and bustedlinks.txt. Knock yourself out."