Home > database >  How to create an XML Parser?
How to create an XML Parser?

Time:12-14

Situation:

I am using Nmap to do network scanning and would like to create a script that takes some Nmap output in XML and parses it and based on that prints the information I want.

Problem Description:

This parser should:

  • Be able to be to take a given input and produce an output

  • The input should be an XML File.

  • The output should be one of these 2 things:

    1. Be able to print text on the terminal
    2. Be able to output processed information to a text file, or an HTML one.

Conclusion:

I need to build a parser that has the above-mentioned functionality, how do I implement it? Or, if possible, is there any ready-built solution that has the required functionality?

Edit 1: I would like to use a bash script, and a bash script is the most preferred answer, but I am open to other languages also.

CodePudding user response:

The first thing that comes to my mind for processing nmap results is Zenmap, but that's more of a GUI for nmap. You might want to check out nmap-parse, it's a commandline tool that seems to provide what you want; although I've never used it myself so I cannot vouch for it.

I should point out that if you are somewhat familiar with Python creating a custom script to parse nmap output is pretty simple. Here is example I created after looking through the documentation of Python xml module and the nmap XML output:

import xml.etree.ElementTree as ET
tree = ET.parse('output.xml')
root = tree.getroot()

for host in filter(lambda h: h.tag == 'host', root):
    status = host.find('status')
    address = host.find('address')
    print('Host', address.attrib['addr'], 'is', status.attrib['state'])
  • Related