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

6次阅读

共计 391 个字符,预计需要花费 1 分钟才能阅读完成。

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
)

运行成果

正文完
 0