Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the PowerShell script netstat to monitor network connectivity

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to use the PowerShell script netstat to monitor the network connection. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

Demand: want to count the data spit out by netstat-na to see if the local network is connected to the external network. Due to some servers can not connect to the external network. If there is a connection to the external network, it may be * *, and you need to call the police.

Note: because netstat is a command of cmd, although you can run this command under PowerShell, the data you spit out cannot be processed again, it is not a PowerShell native command.

Native commands, such as Get-Command, can get all the name values in Get-Command directly from the following script, but not netstat.

Get-Command | Export-Csv-Path c:\ 1\ 2.csv

Import-CSV-Path c:\ 1\ 2.csv | Select-Object Name

So the solution is to spit the data into a CSV file, then process it for CSV, and finally get the statistics you need.

After testing, you need a version of PowerShell3.0 or above, that is, a version of Windows Server 2012 or above (or above windows8).

Script:

Netstat-an | Out-File c:\ 1\ 1.csv

$files = (Get-Childitem c:\ 1\ 1.csv) .pspath

$content = get-content $files | Select-Object-Skip 4

Clear-content $files

Add-Content $files-Value "proto,Local Address,Port1,Foreign Address,Port2,State"

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

$liner = $line.Replace ("127.0.0.1", "local")

$line = $liner

$liner = $line.Replace ("0.0.0.0", "local")

$line = $liner

$liner = $line.Replace ("10.10.14.20", "local")

$line = $liner

$liner = $line.Replace ("*", "outside")

$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

Add-Content $files-Value $liner

}

The above script creates a csv file

The following script triggers an alarm

$emailSmtpServer = "smtp.163.com"

$emailSmtpServerPort = "25"

$emailSmtpUser = "normanjin@163.com"

$emailSmtpPass = "XXXXXXX"

$Body = "there is a connection to the external network, possibly * *!"

$emailFrom = "normanjin@163.com"

$emailTo = "normanjin@163.com"

$content = Import-CSV-Path c:\ 1\ 1.csv | Select "Foreign Address"

Foreach ($line in $content-notlike 'local'-notlike' 10.10'-notlike 'outside'-notlike' 220.181.12.17')

{

$line

}

If ($line-ne $null)

{

$emailMessage = New-Object System.Net.Mail.MailMessage ($emailFrom, $emailTo)

$emailMessage.Subject = "there is a connection to the external network, possibly * *!"

$emailMessage.Body = $Body

$SMTPClient = New-Object System.Net.Mail.SmtpClient ($emailSmtpServer, $emailSmtpServerPort)

$SMTPClient.EnableSsl = $False

$SMTPClient.Credentials = New-Object System.Net.NetworkCredential ($emailSmtpUser, $emailSmtpPass)

$SMTPClient.Send ($emailMessage)

}

The above is how to use the PowerShell script netstat to monitor network connectivity. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report