Pingservertest
This Python script continuously monitors the status of specified servers and firewalls by pinging their IP addresses. Here's a summary and the purpose of the code:
-
IP Address Mapping:
- The script defines two dictionaries:
firewallsandservers, mapping IP addresses to their respective names.
- The script defines two dictionaries:
-
Ping Function:
ping_server(ip_address): This function pings the given IP address once and returnsTrueif the ping is successful (i.e., the server/firewall is up), andFalseotherwise.
-
Monitoring Loop:
print_results(): This function continuously pings all the servers and firewalls, collecting their statuses.- The results are displayed in a formatted table using the
tabulatelibrary. - The status is color-coded: green (
OK) if the server/firewall is reachable, and red (DOWN) if it is not. - The terminal screen is cleared before each update to provide a real-time monitoring effect.
- The script waits for 3 seconds before repeating the checks.
The primary purpose of this script is to monitor the connectivity status of a list of servers and firewalls. By periodically pinging these devices and displaying their statuses, administrators can quickly see which devices are operational and which are not. This real-time monitoring helps in promptly identifying and addressing network issues.
PING LOG FILE
This PowerShell script is designed to periodically ping a list of IP addresses and log the results to a file. Here's a detailed explanation of the code:
$ipAddresses = @{
"Google" = "8.8.8.8"
"ERP" = "XXXXXXXXXXXXX"
"DC1" = "XXXXXXXXXXX"
"FILE" = "XXXXXXXXXX"
"DC2" = "XXXXXXXXXX"
"TSFS" = "XXXXX"
"RDS1" = "XXXXXX"
"RDS2" = "XXXXXXX"
"RDS3" = "XXXXXX"
}- This section defines a hashtable named
$ipAddresseswhere the keys are names (descriptive labels) and the values are the corresponding IP addresses.
$interval = 3600
$logFile = "ping_log.txt"$intervalis set to3600seconds (60 minutes). This determines how often the script will ping the IP addresses.$logFilespecifies the path to the log file where the ping results will be saved.
function PingAndLog($name, $ip, $count) {
Write-Host "Test $count In Progress..."
$result = Test-Connection -ComputerName $ip -Count 60 | Select-Object ResponseTime
$result | ForEach-Object {
$log = "{0}`t{1}`t{2} ms" -f (Get-Date), $name, $_.ResponseTime
Add-Content -Path $logFile -Value $log
}
Write-Host "Test $count Completed Log file results sent"
}PingAndLog($name, $ip, $count)is a function that:- Takes three parameters: the name (label) of the IP address, the IP address itself, and the count of the current test.
- Uses
Test-Connectionto ping the specified IP address 60 times. - Selects only the
ResponseTimefrom the ping results. - Logs the date, name, and response time to the specified log file (
ping_log.txt) with tab-separated values. - Displays messages to the console to indicate the progress and completion of each test.
$count = 1
while ($true) {
$ipAddresses.GetEnumerator() | ForEach-Object {
PingAndLog $_.Key $_.Value $count
}
$count++
Start-Sleep -Seconds $interval
}- Initializes a counter
$countto 1. - Enters an infinite loop (
while ($true)). - Iterates over each key-value pair in the
$ipAddresseshashtable.- Calls
PingAndLogfor each IP address, passing the name, IP address, and current count.
- Calls
- Increments the counter
$count. - Pauses for the specified interval (60 minutes) before repeating the loop.
Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')- Displays a message prompting the user to press any key to exit the script.
- Waits for a key press without displaying the key (
'NoEcho,IncludeKeyDown').
The purpose of this script is to:
- Continuously monitor the connectivity and response times of a list of important IP addresses (servers and services).
- Log the results of these pings to a file for later review.
- Provide a mechanism to monitor network performance and availability over time.
- Allow administrators to identify and troubleshoot connectivity issues based on the logged data.