Created
May 2, 2014 04:45
-
-
Save autumnjolitz/4e806518e965171a36af to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import collections | |
import subprocess | |
import shlex | |
import re | |
import pprint | |
import optparse | |
ips = set(optparse.OptionParser().parse_args()[1]) | |
REGEX = r'\"(.*?)\"|\[(.*?)\]|(\S+)' | |
def analyze(ips): | |
matches = collections.defaultdict(lambda: 0) | |
for ip in ips: | |
if not ip: | |
continue | |
print "Analyzing {0}".format(ip) | |
results = subprocess.Popen(shlex.split('grep {0} access.log'.format(ip)), stdout=subprocess.PIPE).communicate()[0] | |
for line in results.split('\n'): | |
r = map(''.join, re.findall(REGEX, line)) | |
if r: | |
matches[r[-1]] += 1 | |
return dict(matches) | |
print("User Agent analysis:") | |
results = analyze(ips) | |
print("Seen\n") | |
for user_agent, count in results.iteritems(): | |
print("{0}: {1} time{2}".format(user_agent, count, '' if count < 2 else 's')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment