PowerShell統計本地的網路連接(CMD吐資料互訪)

來源:互聯網
上載者:User

標籤:powershell   adsi   active directory域   

需求:想把netstat-an吐出來的資料統計一下,看看本地連外地有多少個,外地連本地有多少個。協議分類什麼的。

注意:因為netstat是cmd的命令,儘管在PowerShell下可以運行這個命令,但是吐出來的資料是沒辦法在加工的,它不是PowerShell原生命令。所以解決方案是把資料吐到一個csv檔案中,然後針對CSV進行加工,最後得到需要的統計資訊。

首先來一個中文版的,經過測試,需要PowerShell3.0以上,也就是Windows Server 2012以上的版本(windows8以上也可以)。

#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 "協議,本地IP地址,連接埠,對端IP地址,對端連接埠,狀態"foreach ($line in $content -ne "活動串連" -ne "  協議  本地地址          外部地址        狀態")   {   $liner = $line.Replace("[::1]","本地");   $line = $liner   $liner = $line.Replace("[::]","本地");   $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","本地")   $line = $liner   $liner = $line.Replace("0.0.0.0","本地")   $line = $liner   $liner = $line.Replace("LISTENING","監聽")   $line = $liner   $liner = $line.Replace("ESTABLISHED","等待")   $line = $liner   $liner = $line.Replace("TIME_WAIT","已建立串連")   $line = $liner   $liner = $line.Replace("CLOSE_WAIT","關閉等待")   $line = $liner   $liner = $line.Replace("SYN_SENT","同步發送")   add-Content $files -Value $liner -Encoding Default      } $bb = Import-Csv -Path d:/3456.csv -Encoding Defaultecho "====================================="echo "以下為協議計數"$bb| Group-Object –Property:協議|select Name,Count|sort -Property countecho "====================================="echo "以下為您本地IP串連外端地址的統計資料"$bb| Group-Object –Property:本地IP地址|select Name,Count|sort -Property countecho "====================================="echo "以下為外端地址串連您本地IP的統計資料"$bb| Group-Object –Property:對端IP地址|select Name,Count|sort -Property countecho "====================================="echo "以下為狀態計數"$bb| Group-Object –Property:狀態|select Name,Count|sort -Property count

再來一個英文版的,因為在PowerShell2.0 也就是Windows Server 2008 R2中,是沒有-Encoding Default參數的,所以要刪了它,改成全英文的描述和說明。所以用中文裝逼是失敗的了。因此建議大家也盡量升級PowerShell到4.0(Windows Server 2008 R2是可以升級PowerShell的)

這裡要說明一下的是,該文檔只適合中文和英文系統,其他語種你需要修改第7行,增加更多-ne的屬性。

#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 "活動串連" -ne "  協議  本地地址          外部地址        狀態")   {   $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","監聽")   $line = $liner   $liner = $line.Replace("ESTABLISHED","等待")   $line = $liner   $liner = $line.Replace("TIME_WAIT","已建立串連")   $line = $liner   $liner = $line.Replace("CLOSE_WAIT","關閉等待")   $line = $liner   $liner = $line.Replace("SYN_SENT","同步發送")#>   add-Content $files -Value $liner       } $files$bb = Import-Csv -Path d:\3456.csv echo "====================================="echo "以下為協議計數"$bb| Group-Object –Property:proto|select Name,Count|sort -Property countecho "====================================="echo "以下為您本地IP串連外端地址的統計資料"$bb| Group-Object –Property:localAddress|select Name,Count|sort -Property countecho "====================================="echo "以下為外端地址串連您本地IP的統計資料"$bb| Group-Object –Property:duiduanip|select Name,Count|sort -Property countecho "====================================="echo "以下為狀態計數"$bb| Group-Object –Property:zhuangtai|select Name,Count|sort -Property count


輸出結果如下

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" />

本文出自 “九叔-微軟私人雲端” 部落格,請務必保留此出處http://jiushu.blog.51cto.com/972756/1663669

PowerShell統計本地的網路連接(CMD吐資料互訪)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.