Python Forum
Receiving XML exception from nmap.scan() results.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Receiving XML exception from nmap.scan() results.
#1
Hi All,

I'm making use of the python-nmap library.

However, running a basic scan against 10.0.0.17/255.255.255.0 or more precisely 10.0.0.17/24, yields the following exception on the returned XML results:

Error:
Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/nmap/nmap.py", line 322, in analyse_nmap_xml_scan dom = ET.fromstring(self._nmap_last_output) File "/usr/lib64/python3.6/xml/etree/ElementTree.py", line 1314, in XML parser.feed(text) File "<string>", line None xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 4, column 1 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "./get_address", line 94, in nmapScan raw = nm.scan(hosts=vlan+'/'+str(cidr), arguments=' -v -sn -n -oG - ') File "/usr/local/lib/python3.6/site-packages/nmap/nmap.py", line 266, in scan nmap_warn_keep_trace=nmap_warn_keep_trace File "/usr/local/lib/python3.6/site-packages/nmap/nmap.py", line 327, in analyse_nmap_xml_scan raise PortScannerError(self._nmap_last_output)
The command is:

raw = nm.scan(hosts=vlan+'/'+str(cidr), arguments=' -v -sn -n -oG - ')
which translates to:

nmap -v -sn -n -oG - 10.0.0.117/24
Raw output would be similar to this:

# nmap -v -sn -n -oG - 10.0.0.117/24
# Nmap 6.40 scan initiated Sun Jan 19 23:22:18 2020 as: nmap -v -sn -n -oG - 10.0.0.117/24
# Ports scanned: TCP(0;) UDP(0;) SCTP(0;) PROTOCOLS(0;)
Host: 10.0.0.0 ()       Status: Down
Host: 10.0.0.1 ()       Status: Up
Host: 10.0.0.2 ()       Status: Down
Host: 10.0.0.3 ()       Status: Down
.
.
.
Host: 10.0.0.252 ()     Status: Down
Host: 10.0.0.253 ()     Status: Down
Host: 10.0.0.254 ()     Status: Down
Host: 10.0.0.255 ()     Status: Down
# Nmap done at Sun Jan 19 23:22:22 2020 -- 256 IP addresses (7 hosts up) scanned in 3.62 seconds
When running the resulting XML through some parsing, I get this (snippet):

Error:
[root@one01 ~]# cat text.txt | awk '{ gsub(/\\n/, "\n", $0); print; }' <?xml version="1.0"?> <?xml-stylesheet href="file:///usr/bin/../share/nmap/nmap.xsl" type="text/xsl"?> <!-- Nmap 6.40 scan initiated Sun Jan 19 23:38:18 2020 as: nmap -oX - -v -sn -n -oG - 10.0.0.117/24 --> # Nmap 6.40 scan initiated Sun Jan 19 23:38:18 2020 as: nmap -oX - -v -sn -n -oG - 10.0.0.117/24 <nmaprun scanner="nmap" args="nmap -oX - -v -sn -n -oG - 10.0.0.117/24" start="1579495098" startstr="Sun Jan 19 23:38:18 2020" version="6.40" xmloutputversion="1.04"> <verbose level="1"/> <debugging level="0"/> # Ports scanned: TCP(0;) UDP(0;) SCTP(0;) PROTOCOLS(0;) <taskbegin task="Ping Scan" time="1579495099"/> <taskend task="Ping Scan" time="1579495102" extrainfo="256 total hosts"/> <host><status state="down" reason="no-response" reason_ttl="0"/> <address addr="10.0.0.0" addrtype="ipv4"/> Host: 10.0.0.0 ()\tStatus: Down </host> <host><status state="up" reason="echo-reply" reason_ttl="255"/> <address addr="10.0.0.1" addrtype="ipv4"/> <hostnames> </hostnames> Host: 10.0.0.1 ()\tStatus: Up <times srtt="3292" rttvar="5000" to="100000"/>
The unaltered error:

Error:
ERROR:root:'<?xml version="1.0"?>\n<?xml-stylesheet href="file:///usr/bin/../share/nmap/nmap.xsl" type="text/xsl"?>\n<!-- Nmap 6.40 scan initiated Sun Jan 19 23:38:18 2020 as: nmap -oX - -v -sn -n -oG - 10.0.0.117/24 -->\n# Nmap 6.40 scan initiated Sun Jan 19 23:38:18 2020 as: nmap -oX - -v -sn -n -oG - 10.0.0.117/24\n<nmaprun scanner="nmap" args="nmap -oX - -v -sn -n -oG - 10.0.0.117/24" start="1579495098" startstr="Sun Jan 19 23:38:18 2020" version="6.40" xmloutputversion="1.04">\n<verbose level="1"/>\n<debugging level="0"/>\n# Ports scanned: TCP(0;) UDP(0;) SCTP(0;) PROTOCOLS(0;)\n<taskbegin task="Ping Scan" time="1579495099"/>\n<taskend task="Ping Scan" time="1579495102" extrainfo="256 total hosts"/>\n<host><status state="down" reason="no-response" reason_ttl="0"/>\n<address addr="10.0.0.0" addrtype="ipv4"/>\nHost: 10.0.0.0 ()\tStatus: Down\n</host>\n<host><status state="up" reason="echo-reply" reason_ttl="255"/>\n<address addr="10.0.0.1" addrtype="ipv4"/>\n<hostnames>\n</hostnames>\nHost: 10.0.0.1 ()\tStatus: Up\n<times srtt="3292" rttvar="5000" to="100000"/>\n</host>\n<host><status state="down" reason="no-response" reason_ttl="0"/>\n<address addr="10.0.0.2" addrtype="ipv4"/>\nHost: 10.0.0.2 ()\tStatus: Down\n</host>\n
Perhaps the -- in the above is causing the XML to throw an exception? Or the #'s ?

Thx,
TK
Reply
#2
I think it's the # - traceback is clear - line 4, column1.
And you can validate your snippet against xml validators and they also raise error on #.
Now - should it be there is another question.
According to their sample output - no, it shouldn't be there
https://nmap.org/book/output-formats-xml-output.html

Note that there is another one on line 8 - # Ports scanned: TCP(0;) UDP(0;) SCTP(0;) PROTOCOLS(0;)
Maybe ask nmap support why it's in their xml output?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Yeah, after I posted I used a validator. Confirmed the same message.

However, the # is expected. There's a few formats nmap can output in, including XML. Meaning, I don't need to use python-nmap's XML generator, using instead an XML reader on nmap results captured from a system command. The option is -oX instead of -oG to nmap. I'll need to research this.

Wouldn't this make it more of a python-nmap coding question then? The #'s invalidate a ton of examples.

Thanks Buran!

Another clue. Looking closely though, line 3, that's the comment on line 4 encoded in HTML comments.

What this tells me is that the python-nmap logic did work converting a comment to HTML format but left the original comment in place as well. Why leave it twice? This suggests the #'s should have been removed once converted to HTML comments. Perhaps I should reach out to the developers of python-nmap to see if there's a way around this?

Thx,
TK
Reply
#4
Maybe you shouldn't supply -oG option in arguments, Nowhere in the examples and the docs they force -oG (i.e. Grepable output format).
Looking at the source code, they force -oX option.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Hm. Tried to use -oX . python-nmap tried to wrap XML into XML.

Now I just tried using without -oG and without -oX. Now it works.

Cheers,
TK
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 1,909 Aug-13-2023, 12:16 AM
Last Post: cubangt
  SMA (simple moving avg) Not receiving Data (stock prices). gdbengo 2 1,407 Jul-31-2022, 08:20 PM
Last Post: paulyan
  Receiving snmp traps with more than one Community String ilknurg 0 2,145 Jan-19-2022, 09:02 AM
Last Post: ilknurg
  Scan for Bluetooth device korenron 0 2,579 Jan-10-2022, 01:06 PM
Last Post: korenron
  Convert looping home security to a one time scan of switches and sensors duckredbeard 0 1,722 Dec-08-2020, 04:31 AM
Last Post: duckredbeard
  scan drives in windows from Cygwin RRR 1 1,603 Nov-29-2020, 04:34 PM
Last Post: Larz60+
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,190 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  [Selenium]Timed out receiving message from renderer: 10.000 wood_6636 0 2,564 Jun-26-2020, 08:59 AM
Last Post: wood_6636
  How to scan huge files and make it in chunks ampai 2 2,542 May-28-2020, 08:20 PM
Last Post: micseydel
  Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. PythonNmap 21 10,635 Jan-19-2020, 07:54 PM
Last Post: PythonNmap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020