Skip to content

Instantly share code, notes, and snippets.

@akagisho
Last active June 10, 2018 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akagisho/a64c7d0e49c2326374b93991f38a7c63 to your computer and use it in GitHub Desktop.
Save akagisho/a64c7d0e49c2326374b93991f38a7c63 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import re
from datetime import datetime
ips = []
count = []
first = []
last = []
for line in sys.stdin:
line = line.rstrip()
m = re.search('\D([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}) .*\[(\d{2}/[A-Z][a-z]{2}/\d{4}:\d{2}:\d{2}:\d{2}) .{0,6}\]', line)
if not m:
continue
ip = m.group(1)
d = datetime.strptime(m.group(2), "%d/%b/%Y:%H:%M:%S")
if ip not in ips:
ips.append(ip)
count.append(0)
first.append(d)
last.append(d)
idx = ips.index(ip)
count[idx] = count[idx] + 1
if first[idx] > d:
first[idx] = d
elif last[idx] < d:
last[idx] = d
for idx in range(len(ips)):
print(ips[idx] + "\t" + str(count[idx]) + "\t" + first[idx].strftime('%Y/%m/%d %H:%M:%S') + "\t" + last[idx].strftime('%Y/%m/%d %H:%M:%S'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment