11#!/usr/bin/env python
2- from libnmap . plugins . backendplugin import NmapBackendPlugin
2+ import json
33from pymongo import MongoClient
44from bson .objectid import ObjectId
55
6+ from libnmap import ReportDecoder , ReportEncoder
7+ from libnmap .plugins .backendplugin import NmapBackendPlugin
8+
69
710class NmapMongoPlugin (NmapBackendPlugin ):
811 def __init__ (self , dbname = None , store = None , ** kwargs ):
@@ -14,8 +17,15 @@ def __init__(self, dbname=None, store=None, **kwargs):
1417 self .dbclient = MongoClient (** kwargs )
1518 self .collection = self .dbclient [self .dbname ][self .store ]
1619
17- def insert (self , dict_data ):
18- self .collection .insert (dict_data )
20+ def insert (self , report ):
21+ # create a json object from an NmapReport instance
22+ j = json .dumps (report , cls = ReportEncoder )
23+ try :
24+ id = self .collection .insert (json .loads (j ))
25+ except :
26+ print "MONGODB cannot insert"
27+ raise
28+ return id
1929
2030 def get (self , report_id = None ):
2131 rid = report_id
@@ -24,6 +34,7 @@ def get(self, report_id=None):
2434
2535 if isinstance (rid , ObjectId ):
2636 r = self .collection .find ({'_id' : rid })
37+ nmapreport = json .loads (r , cls = ReportDecoder )
2738 else :
2839 r = self .collection .find ()
2940 return r
0 commit comments