Search code examples
python-3.xnmapport-scanning

Python Nmap Scanner Progress


So I'm currently using python's nmap library just to get to know the library in python3. The documentation uses nmap.PortScanner() which I have implemented.

My question is, is there a way to determine how far nmap.PortScanner() is within the scanning process, or a way to let the user know that it is still scanning? Within nmap.PortScannerAsync() there is a method to do this, namely: nmap.still_scanning().

I would use nmap.PortScannerAsync(), however I need to use methods within nmap.PortScanner().

Any help would be appreciated.

~Kyhle


Solution

  • So I managed to figure out a solution that seems to work. I'm still going to try and make it better, but I thought I'd put in an interim solution incase someone is struggling with the same issue.

    So my idea was that the scan process would probably need to be in a separate thread if you want to see the progress, since I don't know of a way to hook into the .scan() method. In order to do this the following library was imported:

    from threading import Thread
    

    That was step 1. From there I needed to determine how to actually get the scan process into a thread since I wasn't interested in using multiple threads (I only wanted to see the progress of the scan). The solution that I found to work is shown below:

    scan_thread = Thread(target=network_scan.scan, args=(ip_address, port_range,))
    scan_thread.start()
    while True:
        scan_thread.join(timeout=3)
        if not scan_thread.is_alive():
            break
        print('Nmap is still running...')
    

    This solution is inspired by another post for multithreading which I think I saw on stackoverflow, but I can't find it at the time of posting this.

    Hopefully this helps someone who is in a similar position.

    ~Kyhle