Nmap XML parsing with Powershell

Posted by Craig620 on Server Fault See other posts from Server Fault or by Craig620
Published on 2013-11-06T21:54:03Z Indexed on 2013/11/06 21:56 UTC
Read the original article Hit count: 330

Filed under:
|
|

I am trying to parse the XML output from NMAP and isolate just the hostadddress and the vendor from the osmatch. I've actually done that with the following:

select-xml -path nmap.xml -xpath "nmaprun/host/address/@addr|nmaprun/host/os/osmatch/osclass/@vendor" | select -expandproperty node

Which produces:

#text
-----
10.20.30.1
HP
10.20.30.2
Linux
10.20.30.3
HP

What I was not expecting is that it would jam it all into a single column.
Silly me would like the address in one column, and the vendor in another column.
I Would like:

#addr      #vendor
-----      -------
10.20.30.1 HP
10.20.30.2 Linux
10.20.30.3 HP

In the several hours I spent learning xpath today, I also realized that this file has a single address for each host, but multiple OS guesses for each host. I would also like to use only the first osGuess in the output. Tired using:

-xpath "(nmaprun/host/os/osmatch/osclass/@vendor)[1]"

But that truncates the whole data set to a single line of output, instead of only limiting the only the first osclass element of each host. Changing the parens to surround only the @vendor element like .../(@vendor)[1] and .../(@vendor[1]) but both fail with "Expression must evaluate to a node-set."

Thanks in advance

© Server Fault or respective owner

Related posts about powershell

Related posts about nmap