-1

I am making a network scanner python project and have created below code by looking through youtube learning. But it is not working and giving an error. here is the code-

import nmap
 class network(object):
    def __init__(self):
        ip = input("Enter default IP address 10.10.1.1 10.10.0.1 ")
        self.ip = ip

    def networkscanner(self):
        if len(self.ip) == 0:
            network = '10.10.1.1/24'
        else:
            network = self.ip + '/24'

    print("Start scanning please wait....")

    nm = nmap.Portscanner()
    nm.scan(hosts=network, arguments='-sn')
    hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()]
    for host, status, in hosts_list:
        print("host \t{}".format(host))

    if __name__ == "__main__":
        D = network()
        D.networkscanner()

What I have understood that nmap doesn't have portscanner attributes but not sure though. I have imported python-nmap too and tried but still not working. Can anyone point me to the right direction please?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    *giving an error* What error did you get? What was description of that error? – Daweo Sep 25 '21 at 12:28
  • oops! here it is - Traceback (most recent call last): File "/home/kali/PycharmProjects/networkscanner/main.py", line 2, in class network(object): File "/home/kali/PycharmProjects/networkscanner/main.py", line 15, in network nm = nmap.Portscanner() AttributeError: module 'nmap' has no attribute 'Portscanner' – GhostwhiteActualCompilers Sep 25 '21 at 12:41

2 Answers2

0

According to the documentation, the correct call would be:

nm = nmap.PortScanner()

You have a lower case "s" instead of an upper case.

https://pypi.org/project/python-nmap/

jonnyriv
  • 31
  • 2
0

Ok, so I have managed to run the above code without any error, here are few things I did :

  1. Uninstall both nmap and python-nmap and re installed python-nmap
  2. correct some of the spelling and indentations

and voila! It worked.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 26 '21 at 01:17