如何确定哪个进程正在侦听Windows上的TCP或UDP端口?


当前回答

如果您想使用GUI工具来实现这一点,可以使用Sysinternals的TCPView。

其他回答

获取PID和图像名称

仅使用一个命令:

for /f "tokens=5" %a in ('netstat -aon ^| findstr 9000') do tasklist /FI "PID eq %a"

其中9000应替换为您的端口号。

输出将包含如下内容:

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
java.exe                      5312 Services                   0    130,768 K

说明:

它遍历以下命令输出的每一行:netstat-aon | findstr 9000从每一行中,提取PID(%a-名称在此不重要)(PID是该行中的第5个元素)并传递给以下命令任务列表/FI“PID eq 5312”


如果要跳过标头和命令提示符的返回,可以使用:

echo off & (for /f "tokens=5" %a in ('netstat -aon ^| findstr 9000') do tasklist /NH /FI "PID eq %a") & echo on

输出:

java.exe                      5312 Services                   0    130,768 K

查找使用端口8000的pid

netstat -aon | findstr '8000'

在窗口中终止该进程

taskkill /pid pid /f

其中pid是从first命令中获取的进程id

使用Windows的默认shell(PowerShell),不使用外部应用程序

对于使用PowerShell的用户,请尝试Get NetworkStatistics:

> Get-NetworkStatistics | where Localport -eq 8000


ComputerName  : DESKTOP-JL59SC6
Protocol      : TCP
LocalAddress  : 0.0.0.0
LocalPort     : 8000
RemoteAddress : 0.0.0.0
RemotePort    : 0
State         : LISTENING
ProcessName   : node
PID           : 11552

在编程上,您需要iphlpapi.h中的东西,例如GetTcpTable2()。像MIB_TCP6ROW2这样的结构包含所有者PID。

大多数答案中提到的-b开关要求您具有计算机上的管理权限。您实际上不需要提升权限来获取进程名称!

查找端口号中运行的进程的pid(例如8080)

netstat -ano | findStr "8080"

通过pid查找进程名称

tasklist /fi "pid eq 2216"