使用 NETSTAT 为已建立的活动 TCP 连接获取外部地址名称 [英] Get foreign address name using NETSTAT for established active TCP connections
本文介绍了使用 NETSTAT 为已建立的活动 TCP 连接获取外部地址名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 PowerShell 中使用 NETSTAT 命令.我需要获取以 XYZ 名称开头并使用 TCP 连接建立为状态的外部地址列表.
I'm using NETSTAT command in PowerShell. I need to grab the list of foreign addresses which starts with XYZ name and are ESTABLISHED as state using TCP connections.
由于 NETSTAT 结果如下,我坚持解析.
I stuck up at parsing because of the NETSTAT result as below.
PS C:\Windows\system32>.\NETSTAT.EXE
Active Connections
Proto Local Address Foreign Address State
TCP 10.125.4.233:2310 157.05.39.133:2492 ESTABLISHED
我只需要外部地址名称,而不是本地主机上的远程地址.
远程地址不显示 FQDN.
I need the foreign address names not the remote address on the local host only.
Remote address doesn't show the FQDN.
推荐答案
$netstats = netstat -p TCP -f
$data = $netstats[4..($netstats.count)] #The good info starts on index 4
foreach($line in $data){
$line = $line -split ' ' | ? {$_ -ne ''}
$final += @(New-Object -TypeName psobject -Property @{'Proto'=$line[0];'LAddress'=$line[1];'FAddress'=$line[2];'State'=$line[3]})
}
$netstat_results = $final
$netstat_results | ? {$_.state -eq 'ESTABLISHED'}
现在,在解析 netstat 返回的文本后,应该很容易获取您想要的数据.
Now it should be easy getting the data you want after parsing the text that netstat returns.
这篇关于使用 NETSTAT 为已建立的活动 TCP 连接获取外部地址名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文