Python Forum
Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner.
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner.
#11
I'm editing it in the same folder and user that I'm running it from.

Any way to get more debug info to trace where it's getting it's packages from?
Reply
#12
add following to your script, e.g. inside if __name__ == '__main__': block
quick and dirty:
import os
print(os.path.abspath(nmap.__file__)
or using inspect module from Standard Library:
import inspect
print(inspect.getfile(nmap))
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
#13
Hmm, this is handy. Used this code:

print(nmap.__file__)
and it printed

/usr/local/lib/python3.6/site-packages/nmap/__init__.py
I have python 2.7.X installed as well. The nmap module for that guy is:

/usr/lib/python2.7/site-packages/nmap
[root@server01 nmap]# ls -altri
total 140
 67693355 -rw-r--r--.  1 root root  1316 Jan 25  2016 __init__.py
 67689439 -rw-r--r--.  1 root root   171 Jul 29  2016 test.py
 67689437 -rw-r--r--.  1 root root 12722 Jul 29  2016 test_nmap.py
 67689438 -rw-r--r--.  1 root root 41614 Jul 29  2016 nmap.py
 67693356 -rw-r--r--.  1 root root 13841 Jan 17 09:41 test_nmap.pyc
 67693358 -rw-r--r--.  1 root root   377 Jan 17 09:41 test.pyc
 67693357 -rw-r--r--.  1 root root 32508 Jan 17 09:41 nmap.pyc
 67693359 -rw-r--r--.  1 root root  1467 Jan 17 09:41 __init__.pyc
 67689436 drwxr-xr-x.  2 root root  4096 Jan 17 09:41 .
201526347 drwxr-xr-x. 64 root root  8192 Jan 17 09:41 ..
[root@server01 nmap]#

So switched to python 2.7 temporarily just to see if finds the above.  Now it gives a different error, so looks like it progressed to the next problem:

[python]
$  ./ip-get.py vlan2.xml
/usr/lib/python2.7/site-packages/nmap/__init__.pyc
('[*] Network Address: ', '10.0.0.117')
('[*] Network Mask: ', '255.255.255.0')
NMAP Scanner
Traceback (most recent call last):
  File "./ip-get.py", line 31, in <module>
    main();
  File "./ip-get.py", line 28, in main
    nmapScan(network_address, network_mask)
  File "./ip-get.py", line 14, in nmapScan
    nm = nmap.PortScanner ()
  File "/usr/lib/python2.7/site-packages/nmap/nmap.py", line 131, in __init__
    os.getenv('PATH')
nmap.nmap.PortScannerError: 'nmap program was not found in path. PATH is : /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/var/lib/one/.local/bin:/var/lib/one/bin'
/python]



Thx,
TK

I'll try the second bit of code now. Looks like we were thinking the same.
Reply
#14
Hm. that is strange
you have python-nmap installed for both python2 and python3

so, you shoould have also /usr/local/lib/python3.6/site-packages/nmap/nmap.py file
the __init__.py you show imports everything from it, incl PortScanner.

Can you check that that file is there and also __init__.py file has from .nmap import * line?

you can have a look at the package structure in the source https://xael.org/pages/python-nmap-0.6.1.tar.gz or in the repo https://bitbucket.org/xael/python-nmap/src/default/

As to the error you get in python2 - python-nmap package is wrapper that makes easy to automate running nmap port scanner - https://nmap.org/

i.e. as far as I understand it - you need to install nmap port scanner utility separately
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
#15
(Jan-19-2020, 04:11 PM)buran Wrote: Hm. that is strange
you have python-nmap installed for both python2 and python3

so, you shoould have also /usr/local/lib/python3.6/site-packages/nmap/nmap.py file
the __init__.py you show imports everything from it, incl PortScanner.

Yep, the above file exists.
$ ls -altri /usr/local/lib/python3.6/site-packages/nmap/nmap.py
803233 -rw-r--r--. 1 root root 687 Jan 17 09:38 /usr/local/lib/python3.6/site-packages/nmap/nmap.py
(Jan-19-2020, 04:11 PM)buran Wrote: Can you check that that file is there and also __init__.py file has from .nmap import * line?
$ cat __init__.py
from .nmap import nmap

Ya, looks good:

[user@server01 nmap]$ cat __init__.py
from .nmap import nmap
[user@server01 nmap]$ pwd
/usr/local/lib/python3.6/site-packages/nmap
[user@server01 nmap]$

(Jan-19-2020, 04:11 PM)buran Wrote: you can have a look at the package structure in the source https://xael.org/pages/python-nmap-0.6.1.tar.gz or in the repo https://bitbucket.org/xael/python-nmap/src/default/

As to the error you get in python2 - python-nmap package is wrapper that makes easy to automate running nmap port scanner - https://nmap.org/

i.e. as far as I understand it - you need to install nmap port scanner utility separately

Ya, I see nmap is missing from my system. That's an easy fix I'll do now.

I thought I did install it separately. When you say 'separately', what do you consider 'separately'?

Tried to install python3-nmap, since python3 is the future:

pip search nmap
python3-nmap (1.3.1)     - Python3-nmap converts Nmap commands into python3 methods making it very easy to use nmap in any of your python pentesting projects

pip install python3-nmap
ERROR: Could not find a version that satisfies the requirement python3-nmap (from versions: none)
ERROR: No matching distribution found for python3-nmap
Though it comes up in search, it fails to install. When I switch the interpreter to python3, since I would like to use the latest supported version, I get the original error:

[user@server01 ipa-nmap]$  ./ip-get.py vlan2.xml
/usr/local/lib/python3.6/site-packages/nmap/__init__.py
/usr/local/lib/python3.6/site-packages/nmap/__init__.py
[*] Network Address:  10.0.0.117
[*] Network Mask:  255.255.255.0
NMAP Scanner
Traceback (most recent call last):
  File "./ip-get.py", line 34, in <module>
    main();
  File "./ip-get.py", line 31, in main
    nmapScan(network_address, network_mask)
  File "./ip-get.py", line 17, in nmapScan
    nm = nmap.PortScanner ()
AttributeError: module 'nmap' has no attribute 'PortScanner'
[user@server01 ipa-nmap]$
[user@server01 ipa-nmap]$
[user@server01 ipa-nmap]$
[user@server01 ipa-nmap]$ cat /usr/local/lib/python3.6/site-packages/nmap/__init__.py
from .nmap import nmap
[user@server01 ipa-nmap]$
[user@server01 ipa-nmap]$
[user@server01 ipa-nmap]$ ls -altri /usr/local/lib/python3.6/site-packages/nmap/
total 12
   803233 -rw-r--r--.  1 root root  687 Jan 17 09:38 nmap.py
   803232 -rw-r--r--.  1 root root   23 Jan 17 09:38 __init__.py
   803231 drwxr-xr-x.  3 root root   56 Jan 17 09:38 .
 67689421 drwxr-xr-x.  2 root root   62 Jan 17 09:38 __pycache__
135679242 drwxr-xr-x. 45 root root 4096 Jan 17 09:38 ..
[user@server01 ipa-nmap]$
Thx,
TK
Reply
#16
from .nmap import nmap
that doesn't look right
if you look at the source code, it's

from .nmap import *
and it's even more strange because you should get
Error:
ImportError: cannot import name 'nmap' from 'nmap.nmap'
python-nmap works with python3 - I installed it on my system (linux mint) in virtual environment and I got the same error you got in python2
Error:
nmap.nmap.PortScannerError: 'nmap program was not found in path
I don't have nmap installed on my systems, so it's not available

(Jan-19-2020, 06:54 PM)PythonNmap Wrote: Tried to install python3-nmap, since python3 is the future:
python3-nmap is different package, I didn't look into it

install separately - that it nmap tool should be available on your system, python-nmap will not install it, you should install it yourself if not available

python-nmap will install correctly depending on the python version in which it is installed. As I said I installed it on my python3.7 without any issues
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
#17
Tried nmap3.

#!/bin/python3

import sys
import getopt
import nmap3

import inspect
print(inspect.getfile(nmap3))

print(nmap3.__file__)

import xml.dom.minidom, xml.etree.ElementTree as et

def nmapScan(vlan, netmask):
    # NMAP VLAN to determine IP availability.
    print ("NMAP Scanner")
    nm = nmap3.PortScanner ()


def main():
    xmltree = et.parse(sys.argv[1])

    # XML Tree Item = xti
    for xti in xmltree.iter('AR'):
        network_address = xti.find('NETWORK_ADDRESS').text
        network_mask = xti.find('NETWORK_MASK').text

        print ("[*] Network Address: ", network_address)
        print ("[*] Network Mask: ", network_mask)

    nmapScan(network_address, network_mask)

if __name__ == "__main__":
    main();
But that looks like it's a different animal altogether. It doesn't have the PortScanner function apparently, so that's a valid error message.

[user@server01 ipa-nmap]$  ./ip-get.py vlan2.xml
/usr/local/lib/python3.6/site-packages/nmap3/__init__.py
/usr/local/lib/python3.6/site-packages/nmap3/__init__.py
[*] Network Address:  10.0.0.117
[*] Network Mask:  255.255.255.0
NMAP Scanner
Traceback (most recent call last):
  File "./ip-get.py", line 34, in <module>
    main();
  File "./ip-get.py", line 31, in main
    nmapScan(network_address, network_mask)
  File "./ip-get.py", line 17, in nmapScan
    nm = nmap3.PortScanner ()
AttributeError: module 'nmap3' has no attribute 'PortScanner'
[user@server01 ipa-nmap]$
[user@server01 ipa-nmap]$
[user@server01 ipa-nmap]$
[user@server01 ipa-nmap]$ cd /usr/local/lib/python3.6/site-packages/nmap3/
[user@server01 nmap3]$ grep -EiR PortScanner *
[user@server01 nmap3]$ cd ../nmap
[user@server01 nmap]$ grep -EiR PortScanner *
[user@server01 nmap]$ pwd
/usr/local/lib/python3.6/site-packages/nmap
[user@server01 nmap]$ cd /usr/lib/python2.7/site-packages/nmap/
[user@server01 nmap]$
[user@server01 nmap]$ grep -EiR PortScanner *
Binary file nmap.pyc matches
test_nmap.py:    nm = nmap.PortScanner()
test_nmap.py:@raises(nmap.PortScannerError)
test_nmap.py:    nma = nmap.PortScannerAsync()
test_nmap.py:    nma_ipv6 = nmap.PortScannerAsync()
test_nmap.py:    nmp = nmap.PortScannerAsync()
Binary file test_nmap.pyc matches
test.py:nm = nmap.PortScanner()
Binary file test.pyc matches
[user@server01 nmap]$
Don't see much documentation for nmap3. Basing it on only a few google queries however.
Reply
#18
did you install python-nmap on python3 yourself or it was already installed by someone else? is it possible that someone changed something? uninstall it and install it again
as I said - from .nmap import nmap should not be in the __init__.py

obviously the one in python2 is OK and should work once nmap tool is installed
This is interesting
https://bitbucket.org/xael/python-nmap/i...ortscanner

most of them had the problem because of having their own nmap.py that was creating problems. Original poster however looks like legit problem.
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
#19
I uninstalled all nmap python3 modules I could find and installed the specific python-nmap from python2 onto the python3 libraries, and it appears to have worked like a charm:

# python3 -m pip install python-nmap
WARNING: Running pip install with root privileges is generally not a good idea. Try `__main__.py install --user` instead.
Collecting python-nmap
  Using cached https://files.pythonhosted.org/packages/dc/f2/9e1a2953d4d824e183ac033e3d223055e40e695fa6db2cb3e94a864eaa84/python-nmap-0.6.1.tar.gz
Installing collected packages: python-nmap
  Running setup.py install for python-nmap ... done
Successfully installed python-nmap-0.6.1
Output now is:

$  ./ip-get.py vlan2.xml
/usr/local/lib/python3.6/site-packages/nmap/__init__.py
/usr/local/lib/python3.6/site-packages/nmap/__init__.py
[*] Network Address:  10.0.0.117
[*] Network Mask:  255.255.255.0
NMAP Scanner
$
Full code:

#!/bin/python3

import sys
import getopt
import nmap

import inspect
print(inspect.getfile(nmap))

print(nmap.__file__)

import xml.dom.minidom, xml.etree.ElementTree as et

def nmapScan(vlan, netmask):
    # NMAP VLAN to determine IP availability.
    print ("NMAP Scanner")
    nm = nmap.PortScanner ()


def main():
    xmltree = et.parse(sys.argv[1])

    # XML Tree Item = xti
    for xti in xmltree.iter('AR'):
        network_address = xti.find('NETWORK_ADDRESS').text
        network_mask = xti.find('NETWORK_MASK').text

        print ("[*] Network Address: ", network_address)
        print ("[*] Network Mask: ", network_mask)

    nmapScan(network_address, network_mask)

if __name__ == "__main__":
    main();
So I suppose I can consider this issue resolved now.

Thankx very much!
Reply
#20
just to make it clear - python-nmap supports BOTH 2 and 3, don't make the assumption it's just python2 because it doesn't say python3, like the other package.
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Listening on receiving Interface (using scapy) CodyTheCodeNoob 1 1,894 Dec-22-2024, 10:51 PM
Last Post: PolandoFaker
  Handling receiving updates from user in Telebot mohsenamiri 0 1,512 Aug-26-2024, 09:25 AM
Last Post: mohsenamiri
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 5,609 Aug-13-2023, 12:16 AM
Last Post: cubangt
  SMA (simple moving avg) Not receiving Data (stock prices). gdbengo 2 2,183 Jul-31-2022, 08:20 PM
Last Post: paulyan
  Receiving snmp traps with more than one Community String ilknurg 0 3,821 Jan-19-2022, 09:02 AM
Last Post: ilknurg
  python2 python3 messed up : How to fix ? hary 15 11,560 Dec-30-2020, 08:26 PM
Last Post: hary
  using pudb on python2 code ErnestTBass 2 2,443 Aug-10-2020, 08:12 PM
Last Post: snippsat
  [Selenium]Timed out receiving message from renderer: 10.000 wood_6636 0 3,494 Jun-26-2020, 08:59 AM
Last Post: wood_6636
  output mismatching when porting a python from python2 env to python3 env prayuktibid 2 3,316 Jan-21-2020, 04:41 AM
Last Post: prayuktibid
  Receiving XML exception from nmap.scan() results. PythonNmap 4 5,269 Jan-21-2020, 04:41 AM
Last Post: PythonNmap

Forum Jump:

User Panel Messages

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