Skip to main content

A nmap xml output to json converter

Project description

nmap2json

Convert Nmap XML output to JSON.

nmap2json can be used as a Python library or as a command-line tool. It reads Nmap XML (-oX) and returns one JSON array containing one object per host.

Requirements

  • Python 3.10+

Installation

From this repository:

python -m pip install .

Command-line usage

Generate an Nmap XML report:

nmap -v -A -oX myoutput.xml -p 25,80,443,22 -Pn www.example.org

Print converted JSON to stdout:

python -m nmap2json -i myoutput.xml

Write JSON to a file:

python -m nmap2json -i myoutput.xml -o output.json

Write one JSON file per host, prefixed with the host IP:

python -m nmap2json -i myoutput.xml -o output.json --multiple

Filter output:

python -m nmap2json -i myoutput.xml --notopen
python -m nmap2json -i myoutput.xml --deadhost

CLI help

usage: python3 -m nmap2json [-h] -i INPUT [-o OUTPUT] [-m] [-n] [-d] [--debug]

Convert Nmap XML to JSON

options:
  -h, --help           show this help message and exit
  -i, --input INPUT    Input Nmap XML file
  -o, --output OUTPUT  Output JSON file (prints to stdout if omitted)
  -m, --multiple       Enable multiple JSON outputs (IP prefixed)
  -n, --notopen        Remove from output closed ports
  -d, --deadhost       Remove from output dead hosts
  --debug              Export with smarthash masking

Added fields

Each host object includes extra fields:

{
  "host_reply": true,
  "hsh256": "446c094a24f248da6a87cc7bffaae3df9cf5b0dc5a07d1ca7fff8cdb2071b389"
}
  • host_reply: true when at least one scanned port is open.
  • hsh256: stable SHA-256 hash of the host object, excluding starttime, endtime, and existing hash fields.

Each port also gets its own hsh256 field.

Library usage

Load Nmap XML from a string:

import json
from nmap2json import nmap_xml_to_json

python_obj = nmap_xml_to_json(xml_str)
print(json.dumps(python_obj, indent=2))

Load Nmap XML from a file:

import json
from nmap2json import nmap_file_to_json

python_obj = nmap_file_to_json("myoutput.xml")
print(json.dumps(python_obj, indent=2))

Filter closed ports or dead hosts from library calls:

from nmap2json import nmap_file_to_json

only_open_ports = nmap_file_to_json("myoutput.xml", wipe_notopen=True)
only_live_hosts = nmap_file_to_json("myoutput.xml", wipe_deadhost=True)

License

GNU Affero General Public License v3 or later.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nmap2json-2605.0.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nmap2json-2605.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file nmap2json-2605.0.tar.gz.

File metadata

  • Download URL: nmap2json-2605.0.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for nmap2json-2605.0.tar.gz
Algorithm Hash digest
SHA256 f53fb538458c114120881f4616662af5f249c30f6e46f98dfc921e3563463fbc
MD5 3c661a5a9d7feb0c245aed347d31f4dc
BLAKE2b-256 97b95e60d99e417607e1caefa3e65842dec28d61dc76ab65588610feaeb9c68a

See more details on using hashes here.

File details

Details for the file nmap2json-2605.0-py3-none-any.whl.

File metadata

  • Download URL: nmap2json-2605.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for nmap2json-2605.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a188b3b8e576d8dc0b2f9d63a4ca48ad599d0f5ae4435897bdb791aa178e8f3a
MD5 5ec875de863939cb3d9c07310fbd81c9
BLAKE2b-256 3863d3cc5b5e232af86d72ff434943681727121cda50e26d1ee6290fa685356b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page