How to get the IP address from a nmap output in XML with an specific vendor using ElementTree in Python -


using xml output nmap reachable virtual machines running on host machine - obtained nmap -ox output.xml -sp 192.168.2.*, i'd ip address of each machine vendor matches qemu virtual nic. chose use python's elementtree xml api that, i'm having trouble isolating host elements specified address elements.

here goes snippet of xml output used:

<host><status state="up" reason="arp-response"/> <address addr="192.168.2.93" addrtype="ipv4"/> <address addr="52:54:00:e2:17:31" addrtype="mac" vendor="qemu virtual nic"/> <hostnames> </hostnames> <times srtt="1023" rttvar="5000" to="100000"/> </host> <host><status state="up" reason="arp-response"/> <address addr="192.168.2.96" addrtype="ipv4"/> <address addr="52:54:00:45:86:8a" addrtype="mac" vendor="qemu virtual nic"/> <hostnames> </hostnames> <times srtt="155" rttvar="5000" to="100000"/> </host> <host><status state="up" reason="arp-response"/> <address addr="192.168.2.103" addrtype="ipv4"/> <address addr="52:54:00:61:7a:e5" addrtype="mac" vendor="qemu virtual nic"/> <hostnames> </hostnames> <times srtt="391" rttvar="5000" to="100000"/> </host> 

using findall , xpath syntax below, find address elements have wanted vendor attribute:

import xml.etree.elementtree et tree = et.parse('output.xml') tree.findall("./host/address/[@vendor='qemu virtual nic']") 

but want host elements own address elements found above, can find other address sub-elements of type "ipv4" same host can have host ip address. can point me in right direction achieve using xpath , elementtree?

if must use elementtree (and not lxml)

>>> [i.get('addr') in tree.findall( ...     './host/address[@vendor="qemu virtual nic"]/../address[@addrtype="ipv4"]')] ['192.168.2.93', '192.168.2.96', '192.168.2.103'] 

lxml better library if external dependencies not allowed have do.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -