[python]multi thread tcp connect port scanner
#!/usr/bin/python import optparse from socket import * from threading import * screenLock = Semaphore(value=1) def connScan(host,port): try: s = socket(AF_INET,SOCK_STREAM) s.connect((host,port)) screenLock.acquire() print '[+] '+str(port)+' open' except: screenLock.acquire() print '[-] '+str(port)+' close' finally: screenLock.release() s.close() def portScan(host,ports): try: ip = gethostbyname(host) except: print "[-] Cannot resolve '%s': Unknown host "%host return try: host = gethostbyaddr(ip)[0] print '[+] results for host '+host except: print '[+] results for ip '+ip setdefaulttimeout(1) for port in ports: t = Thread(target=connScan,args=(ip,int(port))) t.start() def main(): parser = optparse.OptionParser('usage %prog -H <target host> -p <target port>') parser.add_option('-H',dest='host',type='string',help='specify target host') parser.add_option('-p',dest='ports',type='string',help='specify target port[s] by comma') (options,args) = parser.parse_args() host = options.host ports = str(options.ports) if (host == None) | (ports == None): print parser.usage exit(0) ports = ports.split(",") portScan(host,ports) if __name__ == "__main__": main()