Skip to content

Instantly share code, notes, and snippets.

@zenfosec
Last active September 18, 2020 15:43
Show Gist options
  • Select an option

  • Save zenfosec/d7bc63308afeabbe97b3 to your computer and use it in GitHub Desktop.

Select an option

Save zenfosec/d7bc63308afeabbe97b3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Takes an input nmap xml file and ouputs a comma separated list of open ports
# This does not include open|filtered ports and is used for an nmap command such as:
#
# nmap -sU -sV -p123,137,161,1645,182 10.10.10.10 -oA udp-services
#
# zenfosec 4/2/14
import xml.etree.ElementTree as ET
import sys
if len(sys.argv) != 2:
print "Usage: portcount.py <nmapresults.xml>"
sys.exit()
openports = []
# opens file and imports as a tree into "root"
tree = ET.parse(sys.argv[1])
root = tree.getroot()
# iterates through tree and append ports to openports array
for child in root:
for host in child:
for port in host:
for status in port:
try:
if status.attrib['state'] == 'open':
port = port.attrib['portid']
if port not in openports:
openports.append(port)
except:
next
nmapportlist = ",".join(openports)
print nmapportlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment