【发布时间】:2019-05-07 10:43:17
【问题描述】:
我正在尝试使用 netstat -bano 并在 PowerShell 中收集输出以满足一些非常具体的报告要求。
我有工作的正则表达式,应该能够解析这个输出没有问题,但是由于输出出现在多行上,正则表达式没有被正确处理
这是 netstat 的截图
期望的输出是这样的(全部在一行上):
TCP 0.0.0.0:135 0.0.0.0:0 监听 1092 RpcSs [svchost.exe] TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4 无法获取所有权信息 TCP 0.0.0.0:623 0.0.0.0:0 监听 7404 [LMS.exe] TCP 0.0.0.0:3389 0.0.0.0:0 监听 1224 TermService [svchost.exe]无法使用 Windows 之外的工具,所以我只限于常用工具。
在 PID 上使用Get-Process 匹配也不起作用,因为它隐藏了 svchost 和 lsass 下的子进程信息。 netstat 和 -b 是完美的,因为它同时显示了 svchost.exe 和使用端口的进程
我已经在互联网上搜寻了一个可行的解决方案,但大多数都以不同的分辨率结束
编辑**这是我使用你们输入的最终脚本
$data = (netstat -bano |select -skip 4 | Out-String) -replace '(?m)^ (TCP|UDP)', '$1' -replace '\r?\n\s+([^\[])', "`t`$1" -replace '\r?\n\s+\[', "`t[" -split "`n"
[regex]$regex = '(?<protocol>TCP|UDP)\s+(?<address>\d+.\d+.\d+.\d+|\[::\]|\[::1\]):(?<port>\d+).+(?<state>LISTENING|\*:\*)\s+(?<pid>\d+)\s+(?<service>Can not obtain ownership information|\[\w+.exe\]|\w+\s+\[\w+.exe\])'
$output = @()
$data | foreach {
$_ -match $regex
$outputobj = @{
protocol = [string]$matches.protocol
address = [string]$matches.address -replace '\[::\]','[..]' -replace '\[::1\]','[..1]'
port = [int]$matches.port
state = [string]$matches.state -replace "\*:\*",'NA'
pid = [int]$matches.pid
service = ([string]$matches.service -replace 'Can not obtain ownership information','[System' -split '.*\[')[1] -replace '\]',''
subservice = ([string]$matches.service -replace 'Can not obtain ownership information','' -split '\[.*\]')[0]
}
$output += New-Object -TypeName PSobject -Property $outputobj
}
$output |select address,port,protocol,pid,state,service,subservice
【问题讨论】:
-
屏幕截图并不总是有帮助;能否将
netstat的输出重定向到一个文件,并将内容作为代码粘贴到问题中? -
我有工作的正则表达式,应该能够解析这个输出没问题。它在哪里?您是否使用单行模式
(?s)之类的标志 -
从搜索“netstat powershell”gallery.technet.microsoft.com/scriptcenter/…
-
我建议使用 Kory 评论中的链接,但在这里...
(netstat -bano) -join "`n" -split "(?= \w\wP\s+(?:\d+\.|\[::))"|%{$_ -replace "`n",' ' -replace '\s{2,}',','}|convertfrom-csv -
@KoryGill 你是对的,它没有考虑任何实际的 IPv6 地址(除了 [::])。确实如此,而且总体上效果更好:
(netstat -bano | Select -skip 2) -join "`n" -split "(?= [TU][CD]P\s+(?:\d+\.|\[\w*:\w*:))"|%{$_.trim() -replace "`n",' ' -replace '\s{2,}',','}|convertfrom-csv
标签: powershell parsing windows-10 netstat