美文网首页
查找端口python脚本

查找端口python脚本

作者: sorry510 | 来源:发表于2020-04-16 20:20 被阅读0次
#!/usr/bin/env python
# -*- coding:utf-8 -*-

# 扫描tcp默认端口0-65534

import socket, time, threading
import sys

socket.setdefaulttimeout(3) #设置默认超时时间
lock = threading.Lock()

def socket_port(ip, port):
    """
    输入IP和端口号,扫描判断端口是否占用
    """
    try:
        if port >= 65535:
            print('端口扫描结束')
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = s.connect_ex((ip, port))
        if result == 0:
            lock.acquire()
            print(ip, ':', port, '端口已开启')
            lock.release()
        else:
            pass
            # print(ip, ':', port, '端口未开启')
    except:
        print('端口扫描异常')

def ip_scan(ip):
    """
    输入IP,扫描IP的0-65534端口情况
    """
    try:
        print('开始扫描 %s' % ip)
        start_time = time.time()
        allThread = []
        for i in range(0, 65534):
            allThread.append(threading.Thread(target=socket_port, args=(ip, int(i))))

        for t in allThread:
            t.start()

        for t in allThread:
            t.join()

        print('扫描端口完成,总共用时:%.2f' % (time.time() - start_time))
    except:
        print('扫描ip出错')

if __name__=='__main__':
    args = sys.argv[1:]
    if len(args) == 0:
        url= input('Input the ip you want to scan: ')
        ip_scan(url)
    else:
        if args[0] == 'help':
            print('run python port.py url')
        else:
            ip_scan(args[0])

相关文章

网友评论

      本文标题:查找端口python脚本

      本文链接:https://www.haomeiwen.com/subject/koadvhtx.html