Created
August 22, 2016 23:42
-
-
Save kkirsche/2fe6de6c8c69ba36843d48f4d500d4d6 to your computer and use it in GitHub Desktop.
Nmap XML to Elasticsearch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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