Last active
June 10, 2018 00:48
-
-
Save akagisho/a64c7d0e49c2326374b93991f38a7c63 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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