Skip to content

Nmap to CSV file – code and explanation

blue retractable pen

The following code uses the xml.etree.ElementTree library to parse the nmap XML output file, then it uses csv library to create a new CSV file, write the headers and write the data to it. It iterates through the XML elements, extracting the relevant data (e.g. hostname, IP address, port number, protocol, service name, state) and writing it to the CSV file.

import xml.etree.ElementTree as ET
import csv

# parse the XML file
tree = ET.parse("nmap_output.xml")
root = tree.getroot()

# create a CSV file and write the headers
with open("nmap_output.csv", "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    headers = ["hostname", "ip", "port", "protocol", "name", "state"]
    writer.writerow(headers)

    # iterate through the XML elements and write the data to the CSV
    for host in root.iter("host"):
        hostname = host.find("hostnames/hostname").attrib["name"]
        ip = host.find("address").attrib["addr"]
        for port in host.iter("port"):
            protocol = port.attrib["protocol"]
            number = port.attrib["portid"]
            state = port.find("state").attrib["state"]
            name = port.find("service").attrib["name"]
            writer.writerow([hostname, ip, number, protocol, name, state])

You can change the input file name to your nmap xml output file and you can change the output file name as well.

Below is an explanation of the above code:

<code>import xml.etree.ElementTree as ET
import csv
</code>

The first two lines import two libraries that are used in this script:

  • xml.etree.ElementTree (also known as ET) is a Python library for parsing and manipulating XML files. It allows you to easily navigate through the XML tree structure and extract data from the elements.
  • csv is a Python built-in library that provides functionality to read and write CSV files.
<code># parse the XML file
tree = ET.parse("nmap_output.xml")
root = tree.getroot()
</code>

This code uses the ET.parse() method to open and parse the Nmap XML output file (“nmap_output.xml”), and it assigns the resulting tree structure to the variable tree. The tree.getroot() method returns the root element of the tree, which is the top-level element of the XML file, and assigns it to the variable root.

<code># create a CSV file and write the headers
with open("nmap_output.csv", "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    headers = ["hostname", "ip", "port", "protocol", "name", "state"]
    writer.writerow(headers)
</code>

This code uses the open() function to create a new CSV file named “nmap_output.csv” in write mode. It also specifies the newline="" argument to prevent the CSV file from adding extra blank lines between rows. The csv.writer() function is used to create a writer object that can write data to the CSV file. The headers variable is a list of strings that represents the headers of the columns of the CSV file: “hostname”, “ip”, “port”, “protocol”, “name” and “state”. The writer.writerow() method writes a single row to the CSV file, in this case it writes the headers.

<code># iterate through the XML elements and write the data to the CSV
    for host in root.iter("host"):
        hostname = host.find("hostnames/hostname").attrib["name"]
        ip = host.find("address").attrib["addr"]
        for port in host.iter("port"):
            protocol = port.attrib["protocol"]
            number = port.attrib["portid"]
            state = port.find("state").attrib["state"]
            name = port.find("service").attrib["name"]
            writer.writerow([hostname, ip, number, protocol, name, state])
</code>

This code uses two nested for loops to iterate through the XML elements:

  • The outer loop iterates through all of the “host” elements in the XML tree using the root.iter("host") method.
  • The inner loop iterates through all of the “port” elements within each “host” element using the host.iter("port") method.

For each host and each port, it extracts the relevant data using the find() and attrib methods:

  • host.find("hostnames/hostname").attrib["name"]

Leave a Reply