命令行关闭特定端口 命令行关闭端口脚本
@echo offset port=8080
set /P port="请输入要关闭的端口:"
echo 开始搜索'netstat -ano^|findstr ":%port%" 'for /f "delims=" %%i in ( 'netstat -ano^|findstr :%port%' ) do set pid_line=%%i
rem set pid=%pid_line:~-6%if "%pid_line%" == "" (
echo 没有找到!
goto flag_exit
) else (
echo 已查找到: %pid_line%
for %%a in (%pid_line%) do (set pid=%%a)
)echo 占用端口%port%的进程pid:%pid%;正在终止改端口...
TASKKILL /PID %pid% /F
:flag_exit
pause
exit;
其他未测试
@echo offchcp 65001set /p port=Please enter the port number to be killed: for /f "tokens=5" %%a in ('netstat -ano ^| findstr "\<0.0.0.0:%port%\>"') do set "pid=%%a"echo Port is %port% → PID is %pid%if not "%pid%"=="" (echo Port %port% found and start killing the process.taskkill /F /PID %pid%
) else (echo Port %port% not found or no associated PID.echo No process to terminate.
)pause
::Windows终止端口脚本
::https://blog.csdn.net/weixin_73754972/article/details/136652228
::https://www.52dixiaowo.com/post-3458.html
::https://blog.51cto.com/u_15002821/4898748
::https://www.cnblogs.com/jpfss/p/11102431.html