killport bat脚本 强制杀死占用端口的进程

https://blog.csdn.net/PiaoMiaoXiaodao/article/details/89792789

@echo off
rem open var delay
setlocal enabledelayedexpansion
rem recev var
set port=%1%
if "%port%" == "" (
echo Please enter port
goto end
)
echo ---------------start find who is using port %port%-----------------------
rem Add colon before port and space after port to ensure no mistake
for /f "tokens=1-5" %%a in ('netstat -ano ^| find ":%port% "') do (
rem a is protocol,b is local ip:port, c is remote ip:port,d is state or pid,e is pid or null
echo %%a %%b %%c %%d %%e
rem Cut the %%b with the : to get the port
for /f "delims=:, tokens=1-2" %%j in ("%%b") do (set destport=%%k)
rem Delay variable in for loop must use! Symbol instead of% symbol
echo The port found here is !destport!. What we need is %port%
rem If %%b cuts the same port as the target port
if "!destport!" equ "%port%" (
if "%%e" == "" (
rem sometimes e is empty pid in d
if not "%%d" == "0" (
echo get d col pid %%d
set pid=%%d
)
) else (
if not "%%e" == "0" (
echo get e col pid %%e
set pid=%%e
)
)
) else (
echo this row local not use port %port%
)
)
echo ---------------end find who is using port %port%-----------------------
if "%pid%"=="" (
echo Port %port% is not in use
) else (
echo used port %port% the process %pid%
taskkill /f /pid %pid%
)

:end
echo ---------------end----------------------------------------------------

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • .bat脚本基本命令语法 目录 批处理的常见命令(未列举的命令还比较多,请查阅帮助信息) 1、REM 和 :: 2...
    庆庆庆庆庆阅读 12,557评论 1 19
  • 一、基础语法: 1.批处理文件是一个“.bat”结尾的文本文件,这个文件的每一行都是一条DOS命令。可以使用任何文...
    蒋_Sir阅读 13,020评论 1 4
  • Windows CMD命令大全 命令简介 cmd是command的缩写.即命令行 。 虽然随着计算机产业的发展,W...
    独行小生阅读 3,701评论 0 5
  • 完全没想到这篇文章的阅读量有点多,之前没有考虑什么就直接发布了,对此表示歉意,所以重新找了一篇文章汇总,以免在坑到...
    BabyFatXu阅读 5,664评论 0 1
  • win7 cmd管理员权限设置 net localgroup administrators 用户名 /add 把“...
    f675b1a02698阅读 10,738评论 0 11