30 Mins

7. Exporting, Parsing & Automating Scan Reports

Exporting Nmap Scan Results

In professional engagements, maintaining a structured record of network discovery is mandatory. Nmap provides several output formats designed for different use cases, ranging from human-readable logs to machine-parsable datasets. The most efficient way to handle these is via the -oA (Output All) flag, which creates three distinct files simultaneously.

The standard formats are: -oN (Normal) for human review, -oX (XML) for programmatic integration, and -oG (Grepable) for legacy shell-based extraction. Below is a comparison of these formats.

FormatDescription / Details
Normal (-oN)Standard text output designed for direct human reading and archiving.
XML (-oX)Structural format designed for database imports and Python parsing.
Grepable (-oG)Single-line per host format optimized for Unix grep, awk, and sed.
Security Operator Note: Always use -oA when scanning client infrastructure to ensure you have a fallback if you need to perform automated analysis later.
Text
nmap -sV -p 80,443 192.168.1.0/24 -oA scan_results

Parsing Grepable Output with Shell Utilities

Grepable output is stored in a format where each line represents one host. Because the data is structured consistently, it is trivial to extract information using standard Linux text processing tools like grep and awk. This allows you to rapidly filter results without needing to write heavy scripts.

For instance, you might want to identify all hosts where port 80 is in an open state from your exported scan file. By piping the output through grep and using awk to print specific columns, you can create a list of target IPs for a secondary automated task.

Security Operator Note: The column numbering in .gnmap files is consistent, but always verify your specific output file structure before running bulk operations.
Text
grep "80/open/tcp" scan_results.gnmap | awk '{print $2}' > live_web_servers.txt

Programmatic XML Parsing with Python

While Grepable format is fine for simple filters, XML is the gold standard for robust automation. Python's xml.etree.ElementTree library allows you to traverse the Nmap report tree to extract specific metadata like service versions, OS signatures, or hostname resolutions.

The following script demonstrates how to parse an Nmap XML file to extract the IP addresses of all hosts where a specific service is detected as open. This method is significantly more reliable than shell scripts when dealing with complex scan reports.

Security Operator Note: Use the python-nmap library if you need to control the Nmap scan engine directly from Python rather than just parsing completed files.
Python 3
import xml.etree.ElementTree as ET

def parse_nmap_xml(file_path):
    tree = ET.parse(file_path)
    root = tree.getroot()
    for host in root.findall('host'):
        address = host.find('address').get('addr')
        ports = host.findall('.//port')
        for port in ports:
            state = port.find('state').get('state')
            if state == 'open':
                print(f'Host: {address} has open port: {port.get('portid')}')

# parse_nmap_xml('scan_results.xml')

Automating Recurring Subnet Discovery

Automation involves wrapping Nmap commands into a crontab or a persistent shell script. This is standard in a SOC environment for 'network hygiene'—tracking when unauthorized devices join a managed subnet.

A simple automation workflow includes executing the scan, verifying if the output file was successfully generated, and archiving the old results. By using timestamped filenames, you can maintain a historical record of network changes over time.

Security Operator Note: Combine this script with diff to compare the current XML report with the previous one to detect new network assets automatically.
Text
#!/bin/bash
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
TARGET="192.168.1.0/24"
OUTPUT_DIR="/opt/voidx/scans"

nmap -sn $TARGET -oX $OUTPUT_DIR/discovery_$TIMESTAMP.xml > /dev/null
echo "Scan completed: discovery_$TIMESTAMP.xml"

Knowledge Check

Ready to test your understanding of 7. Exporting, Parsing & Automating Scan Reports?

Back