暂不支持BAT脚本,以下是Python代码实现:

import socket

# 定义要扫描的主机和端口范围
host = "127.0.0.1"
start_port = 1
end_port = 65535

# 创建一个空列表,用于保存开放的端口
open_ports = []

# 循环遍历端口范围,尝试连接
for port in range(start_port, end_port + 1):
    try:
        # 创建一个socket对象
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # 设置连接超时时间
        s.settimeout(0.5)
        # 尝试连接
        result = s.connect_ex((host, port))
        # 如果连接成功,表示该端口开放
        if result == 0:
            print(f"Port {port} is open")
            open_ports.append(port)
        # 关闭socket连接
        s.close()
    except:
        pass

# 将开放的端口保存到文件中
with open("open_ports.txt", "w") as f:
    for port in open_ports:
        f.write(str(port) + "\n")

运行以上代码后,会输出开放的端口,并将其保存到当前目录下的open_ports.txt文件中。注意,要使用管理员权限运行才能扫描所有端口。

写一个扫描端口开放情况的bat并保存开放的端口

原文地址: http://www.cveoy.top/t/topic/CEs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录