windows 根据端口号关闭所属进程
Junan 2022-03-26
Batch
使用时,把
8080
替换成实际端口号即可
@echo off&setlocal EnableDelayedExpansion
set Port=
set Dstport=8080
for /F "usebackq skip=4 tokens=2,5" %%a in (`"netstat -ano -p tcp"`) do (
for /F "tokens=2 delims=:" %%k in ("%%a") do (
set Port=%%k
)
echo !Port! %%b >>portandpid.txt
)
for /F "tokens=2 delims=:" %%c in ("%1") do (
set Port=%%c
)
for /F "tokens=1,2 delims= " %%d in (portandpid.txt) do (
echo %%d
echo %Dstport%
if %%d == %Dstport% taskkill /f /pid %%e
)
del portandpid.txt
set Port=
set Dstport=
goto :eof
echo '结束了'
pause