bat命令释放指定端口

将以下代码复制粘贴到记事本,后缀重命名为.bat后以管理员身份运行:

@echo off & setlocal EnableDelayedExpansion

title kill port

set /p port(0~65535):

set pid=0
for /f "tokens=2,5" %%b in ('netstat -ano ^| findstr ":%port%"') do (
    set temp=%%b
    for /f "usebackq delims=: tokens=1,2" %%i in (`set temp`) do (
        if %%j==%port% (
            taskkill /f /pid %%c
            set pid=%%c
            echo processes related to port [%port%] are killed
        ) else (
            echo port [%port%] is not occupied on this system
        )
    )
)
if !pid!==0 (
   echo port [%port%] is not occupied
)

echo operation success

pause

输入想要释放的端口号后回车即可。

你可能感兴趣的:(shell,windows,端口扫描,pid,kill)