简体   繁体   English

如何使用Python中的ElementTree与特定供应商从XML的nmap输出中获取IP地址

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

Using the XML output from nmap for the reachable virtual machines running on the host machine - obtained with nmap -oX output.xml -sP 192.168.2.* , I'd like to get the IP address of each machine whose vendor matches QEMU Virtual NIC . 使用nmap的XML输出为主机上运行的可访问虚拟机(通过nmap -oX output.xml -sP 192.168.2.* ,我想获取供应商与QEMU Virtual NIC匹配的每台机器的IP地址QEMU Virtual NIC I chose to use Python's ElementTree XML API to do that, but I'm having trouble isolating the host elements with the specified address elements. 我选择使用Python的ElementTree XML API来执行此操作,但是我无法将主机元素与指定的地址元素隔离开来。

Here it goes a snippet of the XML output to be used: 这里是要使用的XML输出的摘要:

<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 and the XPath syntax below, I could find the address elements which have the wanted vendor attribute: 使用findall和XPath语法,我可以找到具有所需供应商属性的地址元素:

import xml.etree.ElementTree as ET
tree = ET.parse('output.xml')
tree.findall("./host/address/[@vendor='QEMU Virtual NIC']")

But what I really want are the host elements which own the address elements found above, so I can then find the other address sub-elements of type "ipv4" for the same host so I can finally have the host IP address. 但是我真正想要的是拥有上面找到的地址元素的主机元素,因此我可以为同一主机找到类型为“ ipv4”的其他地址子元素,这样我最终可以拥有主机IP地址。 Can anyone point me in the right direction to achieve that using XPath and ElementTree ? 谁能指出我使用XPathElementTree实现该目标的正确方向?

If you must use ElementTree (and not lxml) 如果必须使用ElementTree(而不是lxml)

>>> [i.get('addr') for i 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 is a much better library but if external dependencies are not allowed this will have to do. lxml是一个更好的库,但是如果不允许外部依赖关系,则必须这样做。

The hosts: 主机:

./host[address[@vendor="QEMU Virtual NIC"]]

The IPv4 addresses: IPv4地址:

./host[address[@vendor="QEMU Virtual NIC"]]/address[@addrtype="ipv4"]/@addr

Interactively using lxml : 使用lxml交互:

>>> from lxml import etree
>>> doc = etree.XML("""<doc><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></doc>""")
>>> doc.xpath('./host[address[@vendor="QEMU Virtual NIC"]]')
[<Element host at 0xb72c0af4>, <Element host at 0xb72c0b1c>, <Element host at 0xb72c0b44>]
>>> doc.xpath('./host[address[@vendor="QEMU Virtual NIC"]]/address[@addrtype="ipv4"]/@addr')
['192.168.2.93', '192.168.2.96', '192.168.2.103']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM