XML : Get xmllint xpath nodes to arrays

I have NMAP-output xml-file like this:

  <host><status state="down" reason="no-response" reason_ttl="0"/>  <address addr="192.168.1.1" addrtype="ipv4"/>  </host>  <taskbegin task="SYN Stealth Scan" time="1457545799"/>  <taskend task="SYN Stealth Scan" time="1457545841" extrainfo="1600 total ports"/>  <host starttime="1457545794" endtime="1457545839"><status state="up" reason="echo-reply" reason_ttl="243"/>  <address addr="192.168.1.2" addrtype="ipv4"/>  <hostnames>  <hostname name="192-168-1-2.liibalaaba.com" type="PTR"/>  </hostnames>  <ports><extraports state="closed" count="100">  <extrareasons reason="resets" count="100"/>  </extraports>  </ports>  <times srtt="6454" rttvar="163" to="100000"/>  </host>  <host starttime="1457545794" endtime="1457545837"><status state="up" reason="echo-reply" reason_ttl="51"/>  <address addr="192.168.1.3" addrtype="ipv4"/>  <hostnames>  <hostname name="192-168-1-3.liibalaaba.com" type="PTR"/>  </hostnames>  <ports><extraports state="filtered" count="98">  <extrareasons reason="no-responses" count="98"/>  </extraports>  <port protocol="tcp" portid="80"></port>  <port protocol="tcp" portid="443"></port>  </ports>  <times srtt="6378" rttvar="191" to="100000"/>  </host>  <host starttime="1457545794" endtime="1457545841"><status state="up" reason="echo-reply" reason_ttl="115"/>  <address addr="192.168.1.4" addrtype="ipv4"/>  <hostnames>  <hostname name="192-168-1-4.liibalaaba.com" type="PTR"/>  </hostnames>  <ports><extraports state="filtered" count="97">  <extrareasons reason="no-responses" count="97"/>  </extraports>  <port protocol="tcp" portid="80"></port>  <port protocol="tcp" portid="81"></port>  <port protocol="tcp" portid="443"></port>  </ports>  <times srtt="6417" rttvar="113" to="100000"/>  </host>    

How I can put only the hosts that have open ports to an array and then to readable format? (Do I even need an array?)

Something like this:

  address:192.168.1.3 [0,0]  open ports:80 [0,1], 443 [0,2]  address:192.168.1.4 [1,0]  open ports:80[1,1], 81 [1,2], 443 [1,3]    

Now i'm using xmllint xpath combo, but it just splits out every address that has open ports to same line:

  `xmllint --xpath "//*[local-name()='host']/*[local-name()='address']/@addr | //*[local-name()='ports']/*[local-name()='port']/@portid" nmap-scan.xml >    result.txt`    

Thanks for help!

No comments:

Post a Comment