windows下根据端口号获得进程名

windows下如何根据端口号获取对应的进程名?

思路

1.通过netstat -aon|findstr port寻找端口号对应的进程id,记为pid
2.通过tasklist|findstr pid寻找进程id对应的进程名

代码

@echo off & setlocal EnableDelayedExpansion

:start

set /p port=请输入端口号:
set pid=0

for /f "tokens=5" %%m in ('netstat -aon^|findstr ":%port%"') do (
    set pid=%%m
)
if "!port!" NEQ "0" (
    if "!pid!"=="0" (
        echo 端口号【!port!】没有占用
    ) else (
        for /f "tokens=1" %%n in ('tasklist^|findstr !pid!') do (
            echo 端口号【!port!】进程名为【%%n】
        )
    )
    goto start
)

运行效果

windows下根据端口号获得进程名_第1张图片

你可能感兴趣的:(windows下根据端口号获得进程名)