Skip to content

Instantly share code, notes, and snippets.

@jflyoo
Created February 24, 2021 22:38
Show Gist options
  • Save jflyoo/ac3e727c924367b7e045101f7fe04b0d to your computer and use it in GitHub Desktop.
Save jflyoo/ac3e727c924367b7e045101f7fe04b0d to your computer and use it in GitHub Desktop.
Admin Tools
<#
.SYNOPSIS
nPInG, or New Packet Internet Groper, is a utility to supply live reporting on device connectivity using Powershell on Windows
.DESCRIPTION
NPInG sends ICMP echo requests to a specified device and will report the network connectivity status as it changes
.PARAMETER Destination
Required. Give either the hostname or IP address of the device to query
.PARAMETER TTL
Optional. Default value is 2
.PARAMETER CountBetweenReports
Optional. Default value is 1. Number of queries to make between reports are printed to the screen
#>
param(
[Parameter(Mandatory=$true)] [string]$Destination,
[int32]$TTL=2,
[int32]$CountBetweenReports=1,
[switch]$clean
)
$answered = 0
$unanswered = 0
$reports = 0
#main while loop
while($true)
{
$count = 0
#reporting loop
while($count -lt $CountBetweenReports)
{
if((Test-Connection -ComputerName $Destination -TimeToLive $TTL -quiet))
{
$lastResult = $true
$lastAnswer = Get-Date
$answered++
}
else
{
$lastResult = $false
$unanswered++
}
$count++
} #end reporting loop
#print report
if($clean)
{
cls
Write-Host "NPInG Report"
Write-Host "--------------------------------------------------------"
Write-Host "Answers: $answered"
Write-Host "Last Answer: $lastAnswer" -ForegroundColor Green
Write-Host "Unanswered: $unanswered" -ForegroundColor Red
} #end if $clean
else
{
#if we're not giving a clean report, and the results are filling the screen, remind the user what the target is every 10 probes
if($reports % 10 -eq 0)
{
Write-Host "Target Reminder: $Destination"
}
Write-Host "Answers " -ForegroundColor Green -NoNewline
Write-Host "/ " -NoNewline
Write-Host "Last Answer " -ForegroundColor Green -NoNewline
Write-Host "/ " -NoNewline
Write-Host "Unanswered" -ForegroundColor Red
Write-Host "$answered " -ForegroundColor Green -NoNewline
Write-Host "/ " -NoNewline
Write-Host "$lastAnswer " -ForegroundColor Green -NoNewline
Write-Host "/ " -NoNewline
Write-Host "$unanswered" -ForegroundColor Red
}
Write-Host "--------------------------------------------------------"
if(!$lastResult)
{
if($lastAnswer -ne $null)
{
$now = Get-Date
$tslr = $now - $lastAnswer
Write-Host "Time since last response: " -nonewline
Write-Host $tslr.Hours h $tslr.Minutes m $tslr.Seconds s -ForegroundColor Red
Write-Host "--------------------------------------------------------"
}
else #$lastAnswer isn't initialized; i.e. the script hasn't received any answers since the start
{
Write-Host "Time since last response: Never"
}
Write-Host "--------------------------------------------------------"
}
Write-Host "Ctrl-C to stop script..."
$reports++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment