You are not logged in.

#1 2017-07-31 19:32:18

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Proper way to parse xml into array variables

I wrote a crude unix utils based script that attempts to parse the output of a nmap network scan.  The goal is to show some basic stats of connected devices, ip address, hostname, comments, etc. 

5E:D0:2B:5C:EC:2C  192.168.1.3  Netgear
A8:27:AB:8D:B0:11  192.168.1.101  firewall  Raspberry Pi Foundation
EA:CC:AF:E1:21:2A  192.168.1.102  pi-hole  (unknown)

I found in the nmap man page the ability to write the scan results to an xml file which should allow for proper parsing but the xml tools are new to me.  Can someone recommend a proper syntax to replace my awk/sed hacky script proper calls to xmllint or a like too?

My script: https://github.com/graysky2/bin/blob/ma … sconnected
Example xml: https://gist.github.com/graysky2/8e7f01 … d61017c05e


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2017-07-31 19:52:33

Figgis
Member
Registered: 2015-12-15
Posts: 6

Re: Proper way to parse xml into array variables

Something like this?

xmllint testxml --xpath '//hostnames/hostname/@name'

ntww27T.png

Offline

#3 2017-07-31 20:40:03

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: Proper way to parse xml into array variables

Ah, getting closer... In order to read them into an array (bash), the output would need to be clean hostnames though.  I can obviously use awk or similar but that would defeat the purpose of using the xml utils.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#4 2017-07-31 21:23:58

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Proper way to parse xml into array variables

xmlstarlet sel -t -v '//hostnames/hostname/@name'

Offline

Board footer

Powered by FluxBB