python编写端口扫描工具

"""
端口扫描
"""
import socket
from optparse import OptionParser
from concurrent.futures import ThreadPoolExecutor
# ip = "192.168.10.238"
# port = 445
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))

python编写端口扫描工具_第1张图片

你可能感兴趣的:(python小脚本,python)