Skip to content

Instantly share code, notes, and snippets.

@thomaspatzke
Last active December 12, 2023 13:33
Show Gist options
  • Save thomaspatzke/3d095150fbf52d3e69006aed99fb3e9d to your computer and use it in GitHub Desktop.
Save thomaspatzke/3d095150fbf52d3e69006aed99fb3e9d to your computer and use it in GitHub Desktop.
Extract all open ports in Host:Port format from nmap XML output
xmlstarlet sel -t -m '//port/state[@state="open"]/parent::port' -v 'ancestor::host/address/@addr' -o : -v './@portid' -n nmap-output.xml
@baboures
Copy link

Thanks for the snippet @thomaspatzke! I didn't know about xmlstarlet, which turns out to be a very useful tool for parsing and searching for data through an XML tree. As a side note, your code writes in the format IP:port. If anybody is interested in the format DOMAIN:port, it is as simple as changing:

'ancestor::host/address/@addr'

for:

'ancestor::host/hostnames/hostname[@type="user"]/@name'

See this fork for the whole snippet.

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