windows通过端口杀死进程bat脚本

复制下面代码并创建bat脚本
@echo off
rem 录入端口号找到进程,根据查到的进程ID进行kill

set/p port=请输入杀进程的端口:

echo port : %port%

for /f "usebackq tokens=1-5" %%a in (`netstat -ano ^| findstr %port%`) do (

    if [%%d] EQU [LISTENING] (

        set pid=%%e

    )

)

for /f "usebackq tokens=1-5" %%a in (`tasklist ^| findstr %pid%`) do (

    set executableFile=%%a

)

echo now will kill process : pid %pid%, executableFile: %executableFile%

pause

taskkill /f /pid %pid%

pause