使用来自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元素:
但我真正想要的是拥有上面找到的地址元素的主机元素,这样我就可以为同一主机找到“ipv4”类型的其他地址子元素,这样我就可以最终获得主机IP地址了。有人能告诉我使用XPath和ElementTree实现这一目标的正确方向吗?在
主持人:
IPv4地址:
^{pr2}$交互使用
lxml
:如果必须使用ElementTree(而不是lxml)
lxml
是一个更好的库,但如果不允许外部依赖,则必须这样做。在相关问题 更多 >
编程相关推荐