from socket import *import threading #导入线程相关模块lock = threading.Lock()openNum = 0threads = [] #定义线程列表def portScanner(host,port): global openNum try: s = socket(AF_INET,SOCK_STREAM) s.connect((host,port)) lock.acquire() #因为openNum是个全局变量,每个线程不能对openNum 同时操作,只有获得所的线程才可以操作 openNum openNum+=1 print(‘[+] %d open‘ % port) lock.release() #线程对全局变量openNum操作完成后,需要释放所,其他线程才可以继续修改全局变量openNum s.close() except: #如果端口没开,那么就直接pass,不执行其他输出操作。 passdef main(): setdefaulttimeout(1) ports = [20, 21, 22, 23, 80, 111, 3306] #定义要扫描的端口,也可以在for中使用range进行定义,看个人需求,例如 for p in range(1,1024): for p in ports: t = threading.Thread(target=portScanner,args=(‘192.168.60.130‘,p)) threads.append(t) t.start() for t in threads: #等待线程列表中的所以线程的执行完毕 t.join() print(‘[*] The scan is complete!‘) print(‘[*] A total of %d open port‘ % (openNum))if __name__ == ‘__main__‘: main()
If you want to calculate how long the scan takes, you can import the time module, and then the program starts running minus the last output time when the program ends running.
Python port Scan (full-connection scan, multi-threaded)