forked from bodik/python-libnmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathes.py
More file actions
70 lines (60 loc) · 1.91 KB
/
es.py
File metadata and controls
70 lines (60 loc) · 1.91 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
import json
from datetime import datetime
from elasticsearch import Elasticsearch
from libnmap.plugins.backendplugin import NmapBackendPlugin
from libnmap.reportjson import ReportEncoder
class NmapElasticsearchPlugin(NmapBackendPlugin):
"""
This class enables the user to store and manipulate nmap reports \
in a elastic search db.
"""
def __init__(self, index=None):
if index is None:
self.index = "nmap.{0}".format(datetime.now().strftime("%Y-%m-%d"))
else:
self.index = index
self._esapi = Elasticsearch()
def insert(self, report, doc_type=None):
"""
insert NmapReport in the backend
:param NmapReport:
:return: str the ident of the object in the backend for
future usage
or None
"""
if doc_type is None:
doc_type = "NmapReport"
j = json.dumps(report, cls=ReportEncoder)
res = self._esapi.index(
index=self.index, doc_type=doc_type, body=json.loads(j)
)
rc = res["_id"]
return rc
def delete(self, id):
"""
delete NmapReport if the backend
:param id: str
"""
raise NotImplementedError
def get(self, id):
"""
retreive a NmapReport from the backend
:param id: str
:return: NmapReport
"""
res = self._esapi.get(index=self.index, doc_type="NmapReport", id=id)
rc = res["_source"]
return rc
def getall(self, filter=None):
"""
:return: collection of tuple (id,NmapReport)
:param filter: Nice to have implement a filter capability
"""
rsearch = self._esapi.search(
index=self.index, body={"query": {"match_all": {}}}
)
print("--------------------")
print(type(rsearch))
print(rsearch)
print("------------")