zoukankan      html  css  js  c++  java
  • 04端口扫描工具

    # 简单的端口扫描工具
    # 作者: Charles
    # 公众号: Charles的皮卡丘
    import time
    import socket
    import threading

    # 判断是否为正确的IP地址。
    def isIP(ip):
    ip_addr = ip.split('.')
    if len(ip_addr) != 4:
    return False
    for ipnum in ip_addr:
    if not (0 <= int(ipnum) < 255):
    return False
    else:
    return True


    # 端口扫描工具
    class scanThread(threading.Thread):
    def __init__(self, ip, port_min=0, port_max=65535):
    # 初始化。
    threading.Thread.__init__(self)
    self.port_max = port_max
    self.port_min = port_min
    self.ip = ip
    # assert isinstance(int,self.port_min) and isinstance(int,self.port_max)
    # 重写run
    def run(self):
    return self.__checker()
    # 检测
    def __checker(self):

    for port in range(self.port_min,self.port_max):
    self.__connect(port)
    # 连接
    def __connect(self,port):
    socket.setdefaulttimeout(1)
    self.sock = socket.socket()
    try:
    start_time = time.time()
    self.sock.connect((self.ip,port))
    end_time = time.time()
    connect_time = int(start_time - end_time)
    info = 'Find --> [IP]: %s, [PORT]: %s, [Connect Time]: %d' % (self.ip, port, connect_time)
    print(info)
    self.__save(info)
    self.sock.close()
    except:
    # print('出错误了')
    self.sock.close()
    def __save(self,info):
    try:
    with open('results.txt', 'a') as f:
    f.write(info + ' ')
    except:
    print('写文件出现了问题')
    time.sleep(0.1)

    if __name__ == '__main__':
    # 输入IP地址。
    ip = input('Input IP(example <xxx.xxx.xxx.xxx>): ')
    print(isIP(ip))
    while not isIP(ip):
    ip = input('请输入正确的IP地址: ')
    # 输入最小端口、
    port_min = input('需要扫描的最小端口为:')
    while not (0 <= int(port_min) < 65535):
    port_min = input('请输入正确的需要扫描的最小端口:')
    port_max = input('需要扫描的最大端口为(65535):')
    while not (0 <= int(port_min) < int(port_max) < 65535):
    port_min = input('请输入正确的需要扫描的最大端口(65535):')
    num = 8
    port_max = int(port_max)
    port_min = int(port_min)
    interval = (port_max - port_min) // num
    for i in range(interval):
    scanThread(ip, i * num, (i + 1) * num).start()
  • 相关阅读:
    utools很方便的软件,你的生产力工具集
    uos、deepin安装nfs、查看nfs日志路径
    91--spring cloud (luceneAPI-solr)
    91--spring cloud (Config配置中心--客户端)
    90--spring cloud (Config配置中心)
    90--IDEA整合Git
    解决 IDEA控制台中不能输入数据
    89--spring cloud (zuul-API网关/与feign的比较)
    88--spring cloud (Springboot 整合RabbitMQ)
    88--spring cloud (Turbine聚合监控)
  • 原文地址:https://www.cnblogs.com/cong12586/p/14135468.html
Copyright © 2011-2022 走看看