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

标签 windows powershell

我正在尝试这个小型 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/

相关文章:

python - manage.py 在同一文件夹中继续使用 django 的空白 settings.py 而不是 settings.py manage.py 位于

windows - 动力外壳 $?在做直到循环

powershell - 如何从模块函数中获取退出代码值?

Powershell 无法从命令提示符创建 Outlook COM 对象

python - 安装 pyenv-win 后本地 python 版本没有改变

windows - WinRT 和软件 ClearType 字体渲染

windows - 当线程在 SwapBuffers() 中被阻塞时 GetMessage()

loops - 计算文件中的行数并存储在Variable中

powershell - Pester 没有捕捉到抛出的错误

windows - 用于输出磁盘大小的 Powershell 脚本