Get foreign address name using NETSTAT for established active TCP connections

后端 未结 3 694
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 05:21

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.

<
3条回答
  •  感情败类
    2020-12-20 06:06

     $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'}
    

    Now it should be easy getting the data you want after parsing the text that netstat returns.

提交回复
热议问题