PowerShell statistics local network connection (CMD spit data visits)

Source: Internet
Author: User

Demand: Want to Netstat-an spit out the statistics, to see how many local even the field, the number of foreign even local. Classification of the protocol or something.

Note: Because netstat is a cmd command, although this command can be run under PowerShell, the data spit out is not working, it is not a PowerShell native command. So the solution is to spit the data into a CSV file, then process it for CSV, and finally get the statistics you need.

First of all, a Chinese version, tested, need to PowerShell3.0 above, that is, the version of Windows Server 2012 or more (Windows8 above also can).

#Powered  by kukisama   http://jiushu.blog.51cto.com/netstat -an | out-file -encoding utf8 d:\3456.csv$files =  (get-childitem d:\3456.csv). pspath$ content = get-content  $filesclear-content  $filesadd-content  $files  -Value  " protocol, local IP address, port, peer IP address, peer port, status "foreach  ($line  in  $content  -ne " active connection " -ne "    protocol    Local address            external address           status ")    {    $liner  =  $line. Replace ("[:: 1]", "local");    $line  =  $liner     $liner  =  $line. Replace ("[::]", "local");    $line  =  $liner     $line  =  $liner  -replace ("\s{1,}"  , ",")     $liner  =  $line     $line  =   $liner  -replace (": {1,}"  , " ")     $liner  =  $line     $liner  =  $line. Replace (", TCP", "TCP")     $line  =  $liner     $liner  =  $line. Replace (", UDP," "UDP")     $line  =  $liner     $line  =  $liner  -replace ("\s{1,}"  , ",")     $liner  =  $line     $liner  =   $line. Replace ("127.0.0.1", "local")     $line  =  $liner     $liner  =  $line. Replace ("0.0.0.0", "local")     $line  =  $liner     $liner  =  $line. Replace ("LISTENING", "Listen")     $line  =  $liner     $liner  =  $line. Replace ("Established", "Wait")     $line  =  $liner     $liner  = $ Line. Replace ("Time_wait", "established Connection")     $line  =  $liner     $liner  = $ Line. Replace ("close_wait", "Close Wait") &NBsp;   $line  =  $liner     $liner  =  $line. Replace ("Syn_sent", "Sync send")    add-Content  $files  -Value  $liner  -encoding  Default      }  $BB  = import-csv -path d:/3456.csv  -Encoding Defaultecho  "=====================================" echo  "protocol count below" $BB |  Group-object –property: Protocol |select name,count|sort -property countecho  "=============== ====================== "echo " below is the statistics for your local IP connection external address $BB | group-object –property: local IP address |select  Name,Count|sort -Property countecho  "=====================================" echo  " The following is an external address to connect your local IP statistics "$BB | group-object –property: Peer IP address |select name,count|sort -property  countecho  "=====================================" echo  "the Status count" $bb | group-object – Property: State |select name,count|sort -property  count 

Another English version, because in PowerShell2.0 is the Windows Server R2 , there is no-encoding default parameters, so to delete it, change the description and description of English. So it is a failure to use Chinese to install force. It is recommended that you also upgrade PowerShell to 4.0 (Windows Server R2 can upgrade PowerShell).

Here to illustrate, this document is only suitable for Chinese and English system, other languages you need to modify the 7th line , add more-ne properties.

#Powered  by kukisama   http://jiushu.blog.51cto.com/netstat -an | out-file  d:\3456.csv$files =  (get-childitem d:\3456.csv) .pspath$content =  get-content  $filesclear-content  $filesadd-content  $files  -Value  "proto,localaddress, Duankou,duiduanip,duiduanduanou,zhuangtai "foreach  ($line  in  $content  -ne " active  connections " -ne "   proto  local address           foreign address          State  -ne  Active Connection  -ne     protocol    Local address             external address          status ")    {     $liner  =  $line. Replace ("[:: 1]", "local");    $line  =  $liner     $liner  =  $line.Replace ("[::]", "local");    $line  =  $liner     $line  =  $liner  -replace ("\s{1,}" &NBSP;, ",")     $liner  =  $line     $line  =   $liner  -replace (": {1,}" &NBSP;, " ")     $liner  =  $line     $liner  =  $line. Replace (", TCP", "TCP")     $line  =  $liner     $liner  =  $line. Replace (", UDP," "UDP")     $line  =  $liner     $line  =  $liner  -replace ("\s{1,}" &NBSP;, ",")     $liner  =  $line     $liner  =   $line. Replace ("127.0.0.1", "local")     $line  =  $liner     $liner  = $ Line. Replace ("0.0.0.0", "local")     $line  =  $liner   <#  $liner  =  $line. Replace ("LISTENING", "Listen")     $line  =  $liner     $liner  =  $line. Replace ("Established", "Wait")     $line  =  $liner     $liner  = $ Line. Replace ("Time_wait", "established Connection")     $line  =  $liner     $liner  = $ Line. Replace ("close_wait", "Close Wait")     $line  =  $liner     $liner  = $ Line. Replace ("Syn_sent", "Sync send") #>   add-content  $files  -Value  $liner         }  $files $bb = import-csv -path d:\3456.csv echo   "=====================================" echo  "protocol count below" $BB | group-object –property:proto| select name,count|sort -property countecho  "=====================================" Echo   "Below is the statistics for your local IP connection external address" $BB | group-object –property:localaddress|select name,count|sort  -Property countecho  "=====================================" echo  "The following is an external address to connect your local IP statistics" $BB | Group-Object –Property:duiduanip|select Name,Count|sort -Property countecho  "= = =================================== "echo " below is the status Count $bb | group-object –property:zhuangtai|select  name,count|sort -property count


The output is as follows

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/BB/wKiom1WD3vfz5htzAAFCKGwfCSs330.jpg "title=" 2015-6-19 17-19-52.jpg "alt=" Wkiom1wd3vfz5htzaafckgwfcss330.jpg "/>

This article is from the "Nine uncle-Microsoft Private Cloud" blog, please make sure to keep this source http://jiushu.blog.51cto.com/972756/1663669

PowerShell statistics local network connection (CMD spit data visits)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.