【问题标题】:How to send multiple threads for Nmap scanning in python?如何在python中发送多个线程进行Nmap扫描?
【发布时间】:2018-06-15 04:32:58
【问题描述】:

我有一个小代码,它会做什么,它会通过 Python 找到 nmap 端口扫描。

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     

现在我定义了 2 个函数 nm1nm2 以及另一个函数,即 nmapscan,用于扫描由 nm1nm2 给出的已定义 URL。我已经在两个函数的顶部定义了时间,用于获取线程开始的时间。然后我定义了两个线程,即thth1。我希望两个线程同时启动

现在的问题是,

我不能同时发送两个线程,你可以看到两个函数的时间

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

现在,我已经检查了很多次代码,但我没有发现问题。

【问题讨论】:

    标签: python multithreading python-2.7 python-multithreading


    【解决方案1】:

    python 中的线程库受全局解释器锁的约束。也就是说,即使有多个线程在运行,一般一次也只有一个线程可以执行代码。

    如果您想实现任务的正确并行执行,我建议您改为查看“多处理”库,这样您将有效地创建工作进程并将作业分派给它们。

    【讨论】:

    • 好的,试试看。
    猜你喜欢
    • 2019-02-09
    • 2014-02-16
    • 1970-01-01
    • 2023-02-12
    • 2017-09-13
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多