januu agrawal
januu agrawal

Reputation: 45

How to send multiple threads for Nmap scanning in python?

I have a small code, and what it will do, it will find the nmap port scans through Python.

st = time.time()

urls_returning200 = []
urls_returning400 = []



urls_returning200.append('http://192.168.0.2')
urls_returning400.append('http://192.168.0.3')


ftpurls = []


def nmapscan(ur):

    try:
        nm.scan(ur, '20-25')
        for host in nm.all_hosts():
            print "\nHost: {0} ({1})"  .format(host, nm[host].hostname()) 
            print "Host State:  %s" % nm[host].state()


            for proto in nm[host].all_protocols():
                port = nm[host][proto].keys()
                port.sort()
                print "\nPort      State     Service"
                for ports in port:

                    print "{0}     {1}    {2}" .format(ports, nm[host][proto][ports]['state'], nm[host][proto][ports]['name'])

    except KeyError as e:
        print "[!] Cannot scan host!: " + ur + "\n" 




print "\n...............................................................................................\n"     
print "\n                                                 [PHRASE: 4]: Starts below                                                \n\n"        


nm = nmap.PortScanner()



def nm1():
    global lock
    lock.acquire()
    print time.time()

    try:
        print "\n[!]Finding the Ports stats of 200's URL's \n"
        for x in set(urls_returning200):
            newurl = x.replace('http://', '') 
            nmapscan(newurl)

    finally:
        lock.release()



def nm2():
    global lock
    lock.acquire()
    print time.time()

    try:
        print "\n...............................................................................................\n"
        print "\n[!]Finding the Ports stats of 400's URL's \n"
        for x in set(urls_returning400):
            newurl1 = x.replace('http://', '') 
            nmapscan(newurl1)

    finally:
        lock.release    


lock = threading.Lock()     
if __name__ == '__main__':
    th  = threading.Thread(target=nm1)
    th.start()
    th1 = threading.Thread(target=nm2)
    th1.start()
    th1.join

    print "overall time has been ", time.time()- st     

Now I have defined 2 functions nm1 and nm2 and another function i.e. nmapscan for scanning the defined URLs which are given by the nm1 and nm2. And I have defined the time at the top of both functions for getting on which time the thread starts. And then I defined two threads i.e. th, th1. And I want both threads to start at the same time

Now the problem is,

I can not send the both threads at the same time, You can see the time of both of the functions

1529037892.08 // time of the first function.
[!]Finding the Ports stats of 200's URL's


Host: 192.168.0.2 ()
Host State:  up

Port      State     Service
20     closed    ftp-data
21     closed    ftp
22     closed    ssh
23     closed    telnet
24     closed    priv-mail
25     closed    smtp

...............................................................................................

1529037904.75 // Time of the second function,

[!]Finding the Ports stats of 400's URL's


Host: 192.168.0.4
Host State:  up


overall time has been  31.6859998703

Now, I have check the code so many times but I didn't found the problem.

Upvotes: 1

Views: 3725

Answers (1)

edenist
edenist

Reputation: 11

The threading library within python is bound by the global interpreter lock. That is, that even if multiple threads are running, generally only one thread can execute code at once.

If you want to achieve proper parallel execution of tasks, I would recommend instead looking at the 'multiprocessing' library instead, whereby you will effectively create worker processes and dispatch jobs to them.

Upvotes: 1

Related Questions