-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog2json.py
56 lines (45 loc) · 1.19 KB
/
log2json.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
#!/usr/bin/python -tt
import sys
import json
import re
def add2dict(d, email, ip):
if email in d:
if ip in d[email]:
d[email][ip] += 1
else:
d[email][ip] = 1
else:
d[email] = {ip : 1}
def main():
addr = dict()
data_file = '/tmp/data.json'
start_stop = ['start', 'stop']
# Pop3 SSL log format
# 2017-04-18 00:39:51.793505500 INFO: LOGIN, [email protected], ip=[99.230.45.69]
# Imap4 and IMAP4-SSL format
# 2017-04-17 06:54:31.409298500 INFO: LOGIN, [email protected], ip=[75.98.19.133], protocol=IMAP
# read input into an array
lines = []
for line in sys.stdin:
if 'INFO: LOGIN' in line:
line = line.strip()
lines.append(line)
for line in lines:
fields = line.split(',')
email = fields[1].split('=')[1]
ip = fields[2].split('=')[1][1:-1]
add2dict(addr, email, ip)
ff = lines[0].split()
start_stop[0] = ' '.join(ff[:2])
ff = lines[-1].split()
start_stop[1] = ' '.join(ff[:2])
json_data = {
'addr': addr,
'start_stop': start_stop
}
fout = open(data_file, 'w')
json.dump(json_data, fout)
fout.close()
# print json.dumps(json_data)
if __name__ == "__main__":
main()