Python线程的新功能-它没有';看来没什么不同
我拼凑了一个自动vnc扫描仪。它在IP地址之间循环,如果检测到打开的端口5900,它将尝试屏幕截图。它不漂亮,结构也不好,但它能工作。然而,它是缓慢的。我尝试过线程化进程,但我一直在努力。你可以看到我添加了一个计时器,这样我可以看到扫描30个ip需要多长时间。我尝试了多种类型的线程和线程库。当前的迭代可能是我使用过的最快的迭代,但它只比不使用线程快几秒钟。如果您能提供一些反馈,我将不胜感激 非常感谢Python线程的新功能-它没有';看来没什么不同,python,multithreading,python-multithreading,Python,Multithreading,Python Multithreading,我拼凑了一个自动vnc扫描仪。它在IP地址之间循环,如果检测到打开的端口5900,它将尝试屏幕截图。它不漂亮,结构也不好,但它能工作。然而,它是缓慢的。我尝试过线程化进程,但我一直在努力。你可以看到我添加了一个计时器,这样我可以看到扫描30个ip需要多长时间。我尝试了多种类型的线程和线程库。当前的迭代可能是我使用过的最快的迭代,但它只比不使用线程快几秒钟。如果您能提供一些反馈,我将不胜感激 非常感谢 import socket import nmap from vncdo
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()
导入套接字
导入nmap
从VNCdool导入*
从IP地址导入*
导入预期
导入时间
从多处理导入池中,冻结\u支持
从multiprocessing.dummy导入池作为线程池
进口itertools
def vncconnect(tgtHost):
尝试:
ip=str(tgtHost)
命令='vncdool-v-s'+ip+'--delay=1000捕获%s'(ip+'.jpg')
child=pexpect.spawn(命令)
child.expect('INFO:root:connecting')
时间。睡眠(10)
在“+ip”上打印“正在尝试截图”
child.expect(peexpect.EOF)
除:
通过
def nmapScan(tgtHost、tgtPort):
尝试:
nmScan=nmap.PortScanner()
结果=nmScan.scan(str(tgtHost),str(tgtPort))
如果(结果['nmap']['scanstats']['uphosts']=='1'):
打印“正在尝试”+tgtHost+“-显示为打开状态:正在尝试连接”
vncconnect(tgtHost)
f=打开('数据库','r+'))
f、 写入(tgtHost+'Banner:'+result['scan']['190.81.24.103']['tcp'][5900]['name']+result['scan']['190.81.24.103']['tcp'][5900]/['version']+'/n')
其他:
打印“正在尝试”+tgtHost+“-未打开”
除:
通过
def main():
net4=ipv4地址(u'170.0.0.0')
y=0
开始=时间。时间()
numberofhoststoscan=30
y
它看起来像:
pool.map(main())
。不要调用main
,只需传递对象:pool.map(main)
更新:我将使用a生成要扫描的地址
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(main())
。不要调用main
,只需传递对象:pool.map(main)
更新:我将使用a生成要扫描的地址
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(main())
应该实现什么?它同步调用main()
,然后调用pool.map(无)
。您的代码根本没有使用线程。pool.map(main())应该实现什么?它同步调用main()
,然后调用pool.map(无)
。你的代码根本没有使用线程。pool.map()应该有一个iterable作为第二个参数,可能是ip地址……啊,我明白你的意思了。这是有道理的。把它放在nmap函数中更好吗?嗨,Antti,你建议如何在线程函数中设置IP变量iterable?很多thankspool.map()应该有一个iterable作为第二个参数,可能是ip地址……啊,我明白你的意思了。这是有道理的。把它放在nmap函数中更好吗?嗨,Antti,你建议如何在线程函数中设置IP变量iterable?非常感谢