Skip to content

Instantly share code, notes, and snippets.

@NoifP
Forked from B0073D/nmap_trace_extract.py
Last active March 9, 2023 11:26
Show Gist options
  • Select an option

  • Save NoifP/db9e34ed1d579b9a8ca93286bf48877a to your computer and use it in GitHub Desktop.

Select an option

Save NoifP/db9e34ed1d579b9a8ca93286bf48877a to your computer and use it in GitHub Desktop.
This piece of python will extract hops from the XML output of nmap and write it to a CSV file in a format that Gephi can use.
import xml.etree.ElementTree as etree
tree = etree.parse('wowser.xml')
root = tree.getroot()
hosts = root.findall('host')
output_file = open('nmap_edges.csv', 'w')
output_file.truncate()
output_file.write("Source,Target\n")
for i in hosts:
trace = i.find('trace')
if not (trace == None):
hop_count = 0
for hop in trace:
if hop_count == 0:
last_ip = hop.attrib['ipaddr']
else:
output_file.write(last_ip)
output_file.write(",")
output_file.write(hop.attrib['ipaddr'])
output_file.write("\n")
last_ip = hop.attrib['ipaddr']
hop_count = hop_count + 1
print("Done")
@NoifP
Copy link
Author

NoifP commented Mar 9, 2023

Now works in python3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment