- 1 year ago
Color changing in windows ping
In Windows, you can use the -t
option with the ping
command to continuously ping a target and observe the response times. However, the ping
command itself does not provide built-in options for changing the color of the output. To achieve color changing in the ping
command output, you can use PowerShell to execute the ping
command and apply color formatting to the output.
Here's an example PowerShell script that continuously pings a target and changes the color of the output based on the response time:
powershell$target = "example.com" # Replace with your target hostname or IP address
while ($true) {
$response = ping $target -n 1 | Select-Object -Last 1
$time = [regex]::Match($response, "time=(\d+)")
$responseTime = $time.Groups[1].Value
if ($responseTime -lt 100) {
Write-Host $response -ForegroundColor Green
} elseif ($responseTime -lt 200) {
Write-Host $response -ForegroundColor Yellow
} else {
Write-Host $response -ForegroundColor Red
}
Start-Sleep -Seconds 1
}
In this example, replace "example.com"
with the target hostname or IP address you want to ping. The script continuously pings the target and extracts the response time from the output using regular expressions.
Depending on the response time, the script uses different colors to display the output. Response times less than 100 ms are displayed in green, response times between 100 ms and 200 ms are displayed in yellow, and response times greater than or equal to 200 ms are displayed in red.
To run the script, open PowerShell, copy the code, and paste it into the PowerShell window. Press Enter to start the continuous pinging process with color-changing output.
Note that this script requires PowerShell to be available on your system.