Consider if you will, a world where you have a default nmap XML output.
I am specifically trying to parse out the IP Address (no problem here), and OS Vendor (here is the problem).
The issue is because the xml tag has several instances, as well as attributes and I can't figure out how to use the untangle syntax to pull and attribute from a tag that also needs indices.
The xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="file:///usr/bin/../share/nmap/nmap.xsl" type="text/xsl"?>
<!-- Nmap 7.40 scan initiated Tue Aug 29 12:45:56 2017 as: nmap -sV -O -oX ./nmap_results.xml 1.2.3.4/24 -->
<nmaprun attributes="">
<scaninfo attributes="" />
<debugging attributes="" />
<host attributes="">
<status attributes="" />
<address attributes="" />
<hostnames>
<hostname attributes="" />
</hostnames>
<ports>
<extraports attributes="">
<extrareasons attributes="" />
</extraports>
<port attributes="">
<state attributes="" />
<service attributes="" />
</port>
<port attributes="">
<state attributes="" />
<service attributes="">
<cpe>stuff</cpe>
<cpe>more stuff</cpe>
</service>
</port>
...
Lets just assume that I want to pull the attributes from the first instance of port.
In my python, I would assume it would look something like this:
#!/bin/env python
import untangle
nmap = untangle.parse('./location/to/results.xml')
alive = int(nmap.nmaprun.runstats.hosts['up'])
count = range(0,alive,1)
for tick in count:
print(nmap.nmaprun.host[tick].ports.port[0, 'attribute'])
The problem here, is that instance of port[0, 'attribute']) because it wants and needs that 0 index, but there are also attributes that I want to pull.
Here is the python error:
/usr/bin/python2.7 /path/to/my/dot.py
Traceback (most recent call last):
File "/path/to/my/dot.py", line 10, in <module>
print(nmap.nmaprun.host[tick].ports.port[0, 'vendor'])
TypeError: list indices must be integers, not tuple
Process finished with exit code 1
If I try to just use the attribute name, without the index, I get this:
/usr/bin/python2.7 /path/to/my/dot.py
Traceback (most recent call last):
File "/path/to/my/dot.py", line 10, in <module>
print(nmap.nmaprun.host[tick].ports.port['vendor'])
TypeError: list indices must be integers, not str
Process finished with exit code 1
And if I provide just the index, I get a string with all of the attributes, but I only need to one.
What am I doing wrong or is there a way?