【问题标题】:New to Python Threading - It doesn't appear to make a differencePython 线程新手 - 似乎没有什么不同
【发布时间】:2016-03-10 19:01:35
【问题描述】:

我拼凑了一个自动 vnc 扫描仪。它循环访问 IP 地址,如果检测到开放的 5900 端口,它会尝试截屏。它不漂亮,结构也很差,但它确实有效。然而它很慢。我已经尝试过线程化进程,但我一直在苦苦挣扎。你可以看到我已经添加了一个计时器,所以我可以看到扫描 30 个 ip 需要多长时间。我尝试了多种类型的线程和线程库。当前的迭代可能是我用过的最快的迭代,但它只比没有线程快几秒钟。如果您能提供一些反馈,我将不胜感激。

非常感谢

    import socket
    import nmap
    from vncdotool import *
    from ipaddress import *
    import pexpect
    import time
    from multiprocessing import Pool, freeze_support
    from multiprocessing.dummy import Pool as ThreadPool
    import itertools



    def vncconnect(tgtHost):
        try:
            ip = str(tgtHost)
            command = 'vncdotool -v -s ' + ip + ' --delay=1000 capture %s' % (ip + '.jpg')
            child = pexpect.spawn(command)
            child.expect ('INFO:root:connecting')
            time.sleep (10)
            print 'attempting screenshot on ' + ip
            child.expect (pexpect.EOF)
        except:
            pass

    def nmapScan(tgtHost,tgtPort):

        try:
            nmScan = nmap.PortScanner()
            result = nmScan.scan(str(tgtHost),str(tgtPort))
            if (result['nmap']['scanstats']['uphosts'] == '1'):
            print 'Trying ' + tgtHost + ' - appears open: attempting to connect'
            vncconnect(tgtHost)
            f = open('database', 'r+')
                f.write(tgtHost + ' Banner: ' + result['scan']['190.81.24.103']['tcp'][5900]['name'] + result['scan']['190.81.24.103']['tcp'][5900] /               ['version'] + '/n')

            else:
            print 'Trying ' + tgtHost + ' - is not open'
        except:
            pass


    def main():
        net4 = IPv4Address(u'170.0.0.0')
        y = 0
        start = time.time()
        numberofhoststoscan = 30
        while y < numberofhoststoscan:
            try:            

                port = '5900'
                y = y + 1
                z = str(net4)
                nmapScan(z, port)
                net4 = net4 + 1

            except:
                pass            
                net4 = net4 + 1
        end = time.time()
        total = (end - start)   
        print 'total scan time = ' + str(total) + ', scanned ' + str(numberofhoststoscan) + ' hosts'

    if __name__ == "__main__":
        freeze_support()
        pool = ThreadPool(4)
        pool.map(main())
        pool.close() 
        pool.join()

【问题讨论】:

  • pool.map(main()) 应该实现什么?它同步调用main(),然后调用pool.map(None)
  • 刚刚注意到。您的代码根本没有使用线程。

标签: python multithreading python-multithreading


【解决方案1】:

看起来像:

  1. 在启动线程池之前执行整个扫描:pool.map(main())。不要调用main,直接传递对象:pool.map(main)
  2. 每个线程将开始扫描同一组 IP 地址。您可能希望每个线程扫描一组不同的 IP 地址,以便在它们之间分配工作。


更新:我会使用 generator 来生成要扫描的地址

def addressesToScan(firstAddress, numberofhoststoscan):
    net4 = IPv4Address(firstAddress)
    for y in range(numberofhoststoscan):
        yield net4
        net4 = net4 + 1

要使用它,您需要一个接受地址的函数。要测量总时间,您需要在线程工作人员之外进行测量。

def worker(targetHost):
    port = '5900'
    try:
        nmapScan(targetHost, port)
    except:
        pass

if __name__ == "__main__":
    freeze_support()
    pool = ThreadPool(4)
    start = time.time()
    pool.map(worker, addressesToScan(u'170.0.0.0', 30))
    pool.close()
    pool.join()

    end = time.time()
    total = (end - start)   
    print 'total scan time = ' + str(total) + ', scanned ' + str(numberofhoststoscan) + ' hosts'

【讨论】:

  • pool.map() 应该有一个可迭代的作为第二个参数,可能是 ip 地址...
  • 啊,我明白你的意思了。那讲得通。放在nmap函数里是不是更好?
  • Hi Antti 您如何建议在线程函数中使 IP 变量可迭代?非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多