Python Forum
Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. (/thread-23829.html)

Pages: 1 2 3


Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. - PythonNmap - Jan-19-2020

Hi All,

Given this code snippet, could you please help me understand what am I missing?

High level overview:

1) I've imported python-nmap
2) Imported using import nmap
3) Receiving AttributeError: 'function' object has no attribute 'PortScanner'

Details:

#!/bin/python

import sys
import getopt
import nmap

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

def nmap(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);

    nmap(network_address, network_mask)

if __name__ == "__main__":
    main();
But instead, I'm getting:


Error:
[user@server01 nmap]$ ./get_address vlan2.xml ('[*] Network Address: ', '10.0.0.117') ('[*] Network Mask: ', '255.255.255.0') NMAP Scanner Traceback (most recent call last): File "./get_address", line 113, in <module> main(); File "./get_address", line 107, in main nmap(network_address, network_mask) File "./get_address", line 64, in nmap nm = nmap.PortScanner () AttributeError: 'function' object has no attribute 'PortScanner' [user@server01 nmap]$
I do have python-nmap installed:

[root@server01 nmap]# pip freeze|grep -Ei nmap
python-nmap==0.6.1
[root@server01 nmap]#

Since python2 is no longer supported, tried to use python3 but received the same error. My best interpretation of google search results is that python can't see the installed module. But I'm unable to get further then that.

Cheers,
TK


RE: Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. - buran - Jan-19-2020

what you expect when you name your function nmap, same as the imported module thus overridng the module? Just rename your nmap function...


RE: Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. - PythonNmap - Jan-19-2020

(face palm)

Thanks man. Right, it'll call the local function.

Guess what I was thinking is that it calls a static function on a class and will know that I'm referencing a class static function since I'm using '.' .

I'm just starting with python. Carrying some C++ concepts here. Thank you!

Cheers,
TK


RE: Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. - buran - Jan-19-2020

in python everything is object

def foo():
    pass

foo.bar = 'spam'
print(foo.bar)
print(type(foo))
Output:
spam <class 'function'>
(Jan-19-2020, 02:17 PM)PythonNmap Wrote: will know that I'm referencing a class static function since I'm using '.'

just to make terminology stright - ignoring your function - nmap is package, not class, and PortScanner is a class, so nmap.PortScanner() instantiate object of that class


RE: Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. - PythonNmap - Jan-19-2020

Hmm, I've changed the function name but same result.

#!/bin/python
 
import sys
import getopt
import nmap
 
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();
Error:
$ ./ip-get.py vlan2.xml [*] Network Address: 10.0.0.117 [*] Network Mask: 255.255.255.0 NMAP Scanner Traceback (most recent call last): File "./ip-get.py", line 29, in <module> main(); File "./ip-get.py", line 26, in main nmap(network_address, network_mask) File "./ip-get.py", line 12, in nmap nm = nmap.PortScanner () AttributeError: 'function' object has no attribute 'PortScanner'
Thx,
TK


RE: Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. - buran - Jan-19-2020

did you save the file after the edit? As the traceback show - you still call nmap(network_address, network_mask)
and don't use ; at the end of the line


RE: Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. - PythonNmap - Jan-19-2020

Ya saw that. Meant to come back and edit that since I had the previous error still stuck in the paste buffer. However, this forum only allows that as long as the post is < 10 minutes old.

Took out ';' as well. Same result.

$ cat ip-get.py
#!/bin/python3

import sys
import getopt
import nmap

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();
Error:
$ ./ip-get.py vlan2.xml [*] Network Address: 10.0.0.117 [*] Network Mask: 255.255.255.0 NMAP Scanner Traceback (most recent call last): File "./ip-get.py", line 29, in <module> main(); File "./ip-get.py", line 26, in main nmapScan(network_address, network_mask) File "./ip-get.py", line 12, in nmapScan nm = nmap.PortScanner () AttributeError: module 'nmap' has no attribute 'PortScanner'
Thx,
TK


RE: Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. - buran - Jan-19-2020

no, it's not the same error - now nmap is referenced as module (module 'nmap' has no attribute 'PortScanner') . By any chance do you have another file named nmap.py in the same folder?
If yes - rename/delete it. You import it, not python-nmap package.


RE: Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. - PythonNmap - Jan-19-2020

Nope. Ran this from the location I'm executing the script from:

Error:
$ ls -altri *nmap* ls: cannot access *nmap*: No such file or directory



RE: Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. - buran - Jan-19-2020

not the folder from where you run the script. the folder where the script is. In any case - that's the problem. it does not import the package, it imports nmap.py from somewhere
at least that is what I think.
Here is the docs on python search path, i.e. where it search and in what order when import
https://docs.python.org/3/tutorial/modules.html#the-module-search-path