Skip to content

Instantly share code, notes, and snippets.

@kkirsche
Created August 22, 2016 23:42
Show Gist options
  • Select an option

  • Save kkirsche/2fe6de6c8c69ba36843d48f4d500d4d6 to your computer and use it in GitHub Desktop.

Select an option

Save kkirsche/2fe6de6c8c69ba36843d48f4d500d4d6 to your computer and use it in GitHub Desktop.
Nmap XML to Elasticsearch
import os
import glob
from datetime import datetime
from elasticsearch import Elasticsearch
from libnmap.parser import NmapParser
dir_path = os.path.dirname(os.path.realpath(__file__))
es = Elasticsearch()
for filename in glob.glob(dir_path + '/*.xml'):
nmap_report = NmapParser.parse_fromfile(filename)
for host in nmap_report.hosts:
for serv in host.services:
doc = {
'import_time': datetime.now(),
'ip': host.address,
'service': serv.service,
'port': serv.port,
}
i = 0
for hostname in host.hostnames:
i += 1
doc['hostname{}'.format(i)] = hostname
es.index(index='certificates', doc_type='nmap', body=doc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment