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


Messages In This Thread
Receiving XML exception from nmap.scan() results. - by PythonNmap - Jan-20-2020, 05:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 2,088 Aug-13-2023, 12:16 AM
Last Post: cubangt
  SMA (simple moving avg) Not receiving Data (stock prices). gdbengo 2 1,468 Jul-31-2022, 08:20 PM
Last Post: paulyan
  Receiving snmp traps with more than one Community String ilknurg 0 2,235 Jan-19-2022, 09:02 AM
Last Post: ilknurg
  Scan for Bluetooth device korenron 0 2,631 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,755 Dec-08-2020, 04:31 AM
Last Post: duckredbeard
  scan drives in windows from Cygwin RRR 1 1,641 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,245 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  [Selenium]Timed out receiving message from renderer: 10.000 wood_6636 0 2,656 Jun-26-2020, 08:59 AM
Last Post: wood_6636
  How to scan huge files and make it in chunks ampai 2 2,609 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,957 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