windows下实现统计某个端口的连接数命令及统计端口段连接数脚本
windows下实现统计某个端口的连接数命令:
netstat -an|find "127.0.0.1:218" |find "ESTABLISHED" /c
| 阿里云限量代金券 | 此广告位出租25元/月 |
备注:
1、find命令搜索的字符串要用双引号括起
2、ESTABLISHED是已经建立起的连接,过滤了关闭的,等待的状态的
3、127.0.0.1是本地网卡IP的意思,218是你要统计的端口,根据实际情况修改
脚本1、统计某个端口的连接数脚本:
将下面代码用记事本另存为"检查连接数.bat",编码要采用ANSI保存,否则会出现中文乱码
@echo off
:input_port
cls
set /p "port=请输入要检测的端口 (格式:端口号): "
:: 检测指定端口的连接数
set "ip_address=127.0.0.1"
set "target=%ip_address%:%port%"
set "connection_count=0"
for /f %%i in ('netstat -an ^| find /c "%target%"') do set "connection_count=%%i"
echo 端口 %port% 的连接数为: %connection_count%
:: 等待用户按下回车键继续检测另一个端口或按 'q' 键退出
set /p "choice=按下回车键继续检测另一个端口,按 'q' 键退出: "
if /i "%choice%"=="q" (
exit /b 0
) else (
goto input_port
)脚本2、统计多个端口的连接数脚本:
将下面代码用记事本另存为"检查连接数.bat",编码要采用ANSI保存,否则会出现中文乱码
@echo off
setlocal enabledelayedexpansion
:input_ports
set "ports="
set /p "ports=请输入要检测的端口列表(用空格分隔):"
if "%ports%"=="" (
echo 请输入有效的端口列表。
goto input_ports
)
:count_connections
for %%p in (%ports%) do (
set /a "count=0"
for /f %%i in ('netstat -an ^| find "127.0.0.1:%%p" ^| find "ESTABLISHED" /c') do (
set /a "count+=%%i"
)
echo 端口 %%p 的连接数为: !count!
)
:end
:: 等待用户按下回车键以重新执行或按 'q' 键退出
set /p "choice=按下回车键重新执行,按 'q' 键退出: "
if /i "%choice%"=="q" (
exit /b 0
) else (
goto input_ports
)脚本3、统计端口段连接数脚本(此版本即使连接数为0时也显示结果):
将下面代码用记事本另存为"检查连接数.bat",编码要采用ANSI保存,否则会出现中文乱码
@echo off
setlocal enabledelayedexpansion
:input_ports
set "ports="
set /p "ports=请输入要检测的端口范围 (格式:起始端口-结束端口):"
if "%ports%"=="" (
echo 请输入有效的端口范围。
goto input_ports
)
:count_connections
for /f "tokens=1,2 delims=-" %%a in ("%ports%") do (
set "start_port=%%a"
set "end_port=%%b"
)
for /l %%p in (!start_port!, 1, !end_port!) do (
set /a "count=0"
for /f %%i in ('netstat -an ^| find "127.0.0.1:%%p" ^| find "ESTABLISHED" /c') do (
set /a "count+=%%i"
)
echo 端口 %%p 的连接数为: !count!
)
:end
:: 等待用户按下回车键以重新执行或按 'q' 键退出
set /p "choice=按下回车键重新执行,按 'q' 键退出: "
if /i "%choice%"=="q" (
exit /b 0
) else (
goto input_ports
)脚本4、统计端口段连接数脚本(此版本只会显示有连接数的结果,而忽略没有连接的端口):
将下面代码用记事本另存为"检查连接数.bat",编码要采用ANSI保存,否则会出现中文乱码
@echo off
setlocal enabledelayedexpansion
:input_ports
set "ports="
set /p "ports=请输入要检测的端口范围 (格式:起始端口-结束端口):"
if "%ports%"=="" (
echo 请输入有效的端口范围。
goto input_ports
)
:count_connections
for /f "tokens=1,2 delims=-" %%a in ("%ports%") do (
set "start_port=%%a"
set "end_port=%%b"
)
for /l %%p in (!start_port!, 1, !end_port!) do (
set /a "count=0"
for /f %%i in ('netstat -an ^| find "127.0.0.1:%%p" ^| find "ESTABLISHED" /c') do (
set /a "count+=%%i"
)
if !count! gtr 0 (
echo 端口 %%p 的连接数为: !count!
)
)
:end
:: 等待用户按下回车键以重新执行或按 'q' 键退出
set /p "choice=按下回车键重新执行,按 'q' 键退出: "
if /i "%choice%"=="q" (
exit /b 0
) else (
goto input_ports
)脚本5、从txt文件中读取端口列表统计多个端口的连接数脚本(此版本即使连接数为0时也显示结果):
将下面代码用记事本另存为"检查连接数.bat",编码要采用ANSI保存,否则会出现中文乱码
@echo off
setlocal enabledelayedexpansion
:input_ports
set "ports="
set /p "ports=请输入要检测的端口列表文件名(例如:port.txt):"
if "%ports%"=="" (
echo 请输入有效的端口列表文件名。
goto input_ports
)
if not exist "%ports%" (
echo 文件 "%ports%" 不存在,请确保文件存在。
goto input_ports
)
:count_connections
for /f %%p in (%ports%) do (
set /a "count=0"
for /f %%i in ('netstat -an ^| find "127.0.0.1:%%p" ^| find "ESTABLISHED" /c') do (
set /a "count+=%%i"
)
echo 端口 %%p 的连接数为: !count!
)
:end
:: 等待用户按下回车键以重新执行或按 'q' 键退出
set /p "choice=按下回车键重新执行,按 'q' 键退出: "
if /i "%choice%"=="q" (
exit /b 0
) else (
goto input_ports
)脚本6、从txt文件中读取端口列表统计多个端口的连接数脚本(此版本只会显示有连接数的结果,而忽略没有连接的端口):
将下面代码用记事本另存为"检查连接数.bat",编码要采用ANSI保存,否则会出现中文乱码
@echo off
setlocal enabledelayedexpansion
:input_ports
set "ports="
set /p "ports=请输入要检测的端口列表文件名(例如:port.txt):"
if "%ports%"=="" (
echo 请输入有效的端口列表文件名。
goto input_ports
)
if not exist "%ports%" (
echo 文件 "%ports%" 不存在,请确保文件存在。
goto input_ports
)
:count_connections
for /f %%p in (%ports%) do (
set /a "count=0"
for /f %%i in ('netstat -an ^| find "127.0.0.1:%%p" ^| find "ESTABLISHED" /c') do (
set /a "count+=%%i"
)
if !count! gtr 0 (
echo 端口 %%p 的连接数为: !count!
)
)
:end
:: 等待用户按下回车键以重新执行或按 'q' 键退出
set /p "choice=按下回车键重新执行,按 'q' 键退出: "
if /i "%choice%"=="q" (
exit /b 0
) else (
goto input_ports
)此代码将提示您输入包含端口列表的文件的名称(例如,“port.txt”),然后它将从该文件中读取端口并检查每个端口的连接计数。确保文件“port.txt”存在并包含用空格分隔的端口列表。
txt内容示例:
12000 12001 12087 12061 12055 12093
注意:
如果扫描不出来连接数,就需要将脚本中的127.0.0.1替换成服务器上的IP地址
参考链接:https://blog.csdn.net/GGSDhkn/article/details/126482661




