gpt4 book ai didi

windows - Powershell - 捕获套接字和进程

转载 作者:可可西里 更新时间:2023-11-01 10:38:31 25 4
gpt4 key购买 nike

我正在尝试这个小型 powershell 脚本每五秒查找一次 netstat -ano 输出,然后仅过滤任何 IP 地址上端口 80 上的传出连接,并捕获打开套接字的相关进程。我认为这里的问题是如果输出中有多个条目则它无法处理数组。这里缺少什么?有更好的方法吗?

while(1) {netstat -ano | ? {$_ -like "*10.10.10.10:* *:80 *"} |
% {
$_ -match "\d+$";
$matches | ForEach-Object {
Get-Process -id $matches[0] | Format-List *;
(Get-Process -id $matches[0]).WaitForExit()
}
Start-Sleep -s 5;
}
}

最佳答案

Shay Levy编写了一个函数来处理 netstat 信息,它可以帮助您过滤信息并以更容易过滤的方式输出信息:How to find running processes and their port number

注意:我知道大多数人可能会说要在此处发布代码以防页面丢失。 Shay 正在随着事情的变化或改进(例如添加对 IPv6 连接的支持)更新此页面,所以我怀疑他会很快将其删除。

参见 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

关于windows - Powershell - 捕获套接字和进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11918668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com