-
Notifications
You must be signed in to change notification settings - Fork 1
/
grep-to-json.py
56 lines (44 loc) · 1.36 KB
/
grep-to-json.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/env python3
import fileinput
import json
import sys
fname = sys.argv[1].split('.')
olddate = fname[-2]
d, m, y = olddate.split("-")
date = f"{y}-{m}-{d}"
hosts = dict()
for line in fileinput.input():
if "OS" in line:
print(repr(line))
if line.strip().startswith("#"):
continue
line = line.split("\t")
try:
_, host, ptr = line[0].split(" ")
except ValueError as e:
print("use the .grep file")
sys.exit(1)
if ptr.startswith("(") and ptr.endswith(")"):
ptr = ptr[1:-1]
hosts.setdefault(host, {'host': host, 'ptr': ptr, 'date': date})
for item in line[1:]:
try:
key, value = item.split(": ", 1)
except ValueError as e:
print(repr(item))
raise e
key = key.lower()
value = [v.strip() for v in value.split(",")]
hosts[host].setdefault(key, list())
if key == "ports":
value = [{"ipv4": int(v.split("/")[0]), 'fingerprint': v} for v in value]
# add single items to the list, extend the list otherwise
if len(value) == 0:
hosts[host][key].append(value)
else:
hosts[host][key].extend(value)
json = json.dumps(list(hosts.values()), indent=2)
# write to json file
with open("/tmp/" + fname[0] + "-" + date + ".json", 'w') as f:
f.write(json)
#print(json)