根据端口号来杀死进程

根据端口号获得进程的 pid,然后 taskkill 命令杀死进程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@echo off
:wait_a_port
set port=
echo.&set /p port=Please input port number:
cls
if not defined port goto :wait_a_port

for /f "tokens=2,4,5" %%a in ('netstat -ano^|find /i ":%port% "') do if not "%%c"=="" (set pid=%%c) else (set pid=%%b)
for /f "tokens=1" %%a in ('tasklist /fi "pid eq %pid%"') do set prog=%%a
echo Port Number:%port%
echo PID:%pid%
echo Process:%prog%

set n=
echo.&set /p n=Please input pid to kill it:

if defined n (echo.&taskkill /f /fi "pid eq %pid%")

echo.&echo Press to exit ...
pause>nul
exit