"""
端口扫描
"""
import socket
from optparse import OptionParser
from concurrent.futures import ThreadPoolExecutor
def scan_one(ip,port):
socket.setdefaulttimeout(1)
try:
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((ip,port))
print("[+]%d open"%port)
except:
pass
if __name__ == '__main__':
opt = OptionParser("usage:%prog -i ip -p port")
opt.add_option("-i", "--IP", type='string', dest='ip', help="scan ip add")
opt.add_option('-p', "--PORT", type='string', dest="port", help="scan port")
opt.add_option('-t',"--THREAD",type='int',dest="threads",help="thread number")
options, args = opt.parse_args()
ip = options.ip
ports = options.port
if "-" in ports:
start_port = int(ports.split("-")[0])
end_port = int(ports.split("-")[1])
thread_num = options.threads
with ThreadPoolExecutor(max_workers=thread_num) as pool:
for port in range(start_port,end_port+1):
pool.submit(scan_one,ip,port)
else:
scan_one(ip,int(ports))