我将以下nmap输出作为xml格式:
<ports><extraports state="closed" count="991">
<extrareasons reason="conn-refused" count="991"/>
</extraports>
<port protocol="tcp" portid="22"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ssh" method="table" conf="3"/></port>
<port protocol="tcp" portid="25"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="smtp" method="table" conf="3"/></port>
<port protocol="tcp" portid="139"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="netbios-ssn" method="table" conf="3"/></port>
<port protocol="tcp" portid="443"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="https" method="table" conf="3"/></port>
我想获得打开的端口号:
print 'Port Number: '+host.find('ports').find('port').get('portid')
但结果只是22
。
我如何获得结果:
22
25
139
443
答案 0 :(得分:1)
查找所有port
个元素,并获取portid
个属性。
使用Element.findall
和list comprehension:
>>> import xml.etree.ElementTree as ET
>>> root = ET.fromstring('''
<ports><extraports state="closed" count="991">
<extrareasons reason="conn-refused" count="991"/>
</extraports>
<port protocol="tcp" portid="22"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ssh" method="table" conf="3"/></port>
<port protocol="tcp" portid="25"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="smtp" method="table" conf="3"/></port>
<port protocol="tcp" portid="139"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="netbios-ssn" method="table" conf="3"/></port>
<port protocol="tcp" portid="443"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="https" method="table" conf="3"/></port>
</ports>
''')
>>> [port.get('portid') for port in root.findall('.//port')]
['22', '25', '139', '443']