如何使用Python中的ElementTree从XML格式的nmap输出中获取IP地址

2024-10-02 18:16:34 发布

您现在位置:Python中文网/ 问答频道 /正文

使用来自nmap的XML输出,用于主机上运行的可访问虚拟机(通过nmap -oX output.xml -sP 192.168.2.*获得),我想获得供应商与QEMU Virtual NIC匹配的每台机器的IP地址。我选择使用Python的ElementTree XML API来实现这一点,但是在用指定的地址元素隔离主机元素时遇到了困难。在

下面是要使用的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>

使用findall和下面的XPath语法,我可以找到具有wanted vendor属性的address元素:

^{pr2}$

但我真正想要的是拥有上面找到的地址元素的主机元素,这样我就可以为同一主机找到“ipv4”类型的其他地址子元素,这样我就可以最终获得主机IP地址了。有人能告诉我使用XPathElementTree实现这一目标的正确方向吗?在


Tags: host元素address地址statusvirtualxmladdr
2条回答

主持人:

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

IPv4地址:

^{pr2}$

交互使用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']

如果必须使用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是一个更好的库,但如果不允许外部依赖,则必须这样做。在

相关问题 更多 >