Nmap to CSV file — code and explanation

Hacksheets | Learn Cybersecurity
3 min readJan 20, 2023

--

Nmap to CSV file - code and explanation

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 =

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

ip = host.find(“address”).attrib

for port in host.iter(“port”):

protocol = port.attrib

number = port.attrib

state = port.find(“state”).attrib

name = port.find(“service”).attrib

writer.writerow()

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:

import xml.etree.ElementTree as ET

import csv

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.

# parse the XML file

tree = ET.parse(“nmap_output.xml”)

root = tree.getroot()

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.

# create a CSV file and write the headers

with open(“nmap_output.csv”, “w”, newline=””) as csvfile:

writer = csv.writer(csvfile)

headers =

writer.writerow(headers)

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.

# iterate through the XML elements and write the data to the CSV

for host in root.iter(“host”):

hostname = host.find(“hostnames/hostname”).attrib

ip = host.find(“address”).attrib

for port in host.iter(“port”):

protocol = port.attrib

number = port.attrib

state = port.find(“state”).attrib

name = port.find(“service”).attrib

writer.writerow()

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

--

--

Hacksheets | Learn Cybersecurity
Hacksheets | Learn Cybersecurity

Written by Hacksheets | Learn Cybersecurity

Ethical Hacking & Information Security Cheatsheets, Resources, Tools, Quizzes, and lots of free learning material.

No responses yet