Skip to content

Instantly share code, notes, and snippets.

@numanturle
Created April 1, 2022 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numanturle/7390cb54b734f52e242d94f0e2be1dbc to your computer and use it in GitHub Desktop.
Save numanturle/7390cb54b734f52e242d94f0e2be1dbc to your computer and use it in GitHub Desktop.
python parser nmap
#!/usr/bin/python
import sys
import argparse
import xml.etree.ElementTree as ET
def main(argv):
inputfile = ''
outputfile = ''
parser = argparse.ArgumentParser(description="Parse Nmap XML output and create CSV")
parser.add_argument('inputfile', help='The XML File')
parser.add_argument('outputfile', help='The output csv filename')
parser.add_argument('-n', '--noheaders', action='store_true', help='This flag removes the header from the CSV output File')
args = parser.parse_args()
inputfile=args.inputfile
outputfile = args.outputfile
try:
tree = ET.parse(inputfile)
root = tree.getroot()
except ET.ParseError as e:
print("Parse error({0}): {1}".format(e.errno, e.strerror))
sys.exit(2)
except IOError as e:
print("IO error({0}): {1}".format(e.errno, e.strerror))
sys.exit(2)
except:
print("Unexpected error:", sys.exc_info()[0])
sys.exit(2)
fo = open(outputfile, 'w+')
if (args.noheaders != True):
out = "ip" + ';' + "hostname" + ';' + "port" + ';' + "protocol" + ';' + "service" + ';' + "version" + ';\n'
fo.write (out)
for host in root.findall('host'):
ip = host.find('address').get('addr')
hostname = ""
if host.find('hostnames') is not None:
if host.find('hostnames').find('hostname') is not None:
hostname = host.find('hostnames').find('hostname').get('name')
for port in host.find('ports').findall('port'):
protocol = port.get('protocol')
if protocol is None:
protocol = ""
portnum = port.get('portid')
if portnum is None:
portnum = ""
service = ""
if port.find('service') is not None:
if port.find('service').get('name') is not None:
service = port.find('service').get('name')
product = ""
version = ""
versioning = ""
extrainfo = ""
if port.find('service') is not None:
if port.find('service').get('product') is not None:
product = port.find('service').get('product')
versioning = product.replace(",", "")
if port.find('service').get('version') is not None:
version = port.find('service').get('version')
versioning = versioning + ' (' + version + ')'
if port.find('service').get('extrainfo') is not None:
extrainfo = port.find('service').get('extrainfo')
versioning = versioning + ' (' + extrainfo + ')'
out = ip + ';' + hostname + ';' + portnum + ';' + protocol + ';' + service + ';' + versioning + ';\n'
fo.write (out)
fo.close()
if __name__ == "__main__":
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment