DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Ping with latency alarm?  (Read 19,371 times)

timmy2

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 28
Ping with latency alarm?
« on: May 15, 2021, 01:49 AM »
I am looking for a program that can ping an address every 2 seconds and play a sound when the return time exceeds a certain value. It needs to display a timestamp and return time for every ping so I can capture this output to a text file, which I'll then import into Excel.

IainB

  • @Slartibartfarst
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,544
Re: Ping with latency alarm?
« Reply #1 on: May 15, 2021, 08:16 AM »
The simplest way to do this might be to write/use a PowerShell script.
You could set the alarm value as a variable for testing with an IF statement, or just dump all the pin responses into (say) an Excel spreadsheet or Access database for analysis.
Refer for example: https://www.checkyourlogs.net/ping-sweep-using-powershell-quick-and-dirty-powershell-mvphour/

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Ping with latency alarm?
« Reply #2 on: May 15, 2021, 07:13 PM »
Seems like a good idea for a coding snack.

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Ping with latency alarm?
« Reply #3 on: May 17, 2021, 12:31 PM »
Someone will come up with something fancier but it gave me something to do  :)

It's only requirement is PowerShell 7 or higher, (I no longer use earlier versions).

Doesn't write to console, just directly to the output file.

Code: ( powershell)
<#
  pinger.ps1

  Run: pwsh -F path\to\pinger.ps1
#>

$ip = "8.8.8.8"       # IP/site to ping
$file = "R:\ping.csv" # Output file, (will be output as CSV)
$maxlag = 40          # Latency above which to go beep

# If it ain't PowerShell 7+ we don't want to know about it.
if ($PSVersionTable.PSVersion.Major -lt 7) {
  "Requires PowerShell 7" | Out-Host
  Start-Sleep 10
} else {
  While ($true) {  # Loop-de-loop
    # Ping a site once and select latency
    $lag = (Test-Connection -ComputerName $ip -Count 1 -Ping -IPv4 -DontFragment | Select-Object Latency)
    # Format file output and append latest to file (date time,ip,latency,exceeds max)
    "$(Get-Date -Format 'yyyy/MM/dd HH:mm:ss'),$($ip),$($lag.Latency),$($lag.Latency -gt $maxlag)" | Out-File $file -Append
    # If latency is above maximum allowed value ...
    if ($lag.Latency -gt $maxlag) {
      # Go beep
      [console]::beep(1000,500)
    }
    # Sleep, restful sleep ... for all of 2 seconds >:|
    Start-Sleep -Seconds 2
  }
}


File output is in CSV format:
Code: (text)
2021/05/17 22:22:35,8.8.8.8,6,False
2021/05/17 22:22:37,8.8.8.8,17,False
2021/05/17 22:22:39,8.8.8.8,41,True
2021/05/17 22:22:42,8.8.8.8,32,False
2021/05/17 22:22:44,8.8.8.8,32,False
2021/05/17 22:22:46,8.8.8.8,41,True
2021/05/17 22:22:48,8.8.8.8,33,False
2021/05/17 22:22:51,8.8.8.8,44,True
2021/05/17 22:22:53,8.8.8.8,43,True
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

GrumpyCoder

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 37
    • Participated in NANY Challenge Five Times
    • Writer
Re: Ping with latency alarm?
« Reply #4 on: December 15, 2022, 05:28 PM »
I didn't realize how old this thread is before I started working on a solution. In any case, it's already done, so here it is anyway.
I believe this should do what you wanted, but do note that I whipped it up in a couple of hours, so it's definitely not perfect.
PS: The "csv" file is saved right next to the executable.

Capture.PNG

hamradio

  • Amateur Radio Guy
  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 893
    • Plugin Coding Medal
    • Participated in NANY Challenge Fifteen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
    • Hardware Hacker
Re: Ping with latency alarm?
« Reply #5 on: December 28, 2022, 06:56 PM »
I didn't realize how old this thread is before I started working on a solution. In any case, it's already done, so here it is anyway.
I believe this should do what you wanted, but do note that I whipped it up in a couple of hours, so it's definitely not perfect.
PS: The "csv" file is saved right next to the executable.

[attachment 2 not found]

I am pretty sure this could be classified as a NANY App this year I believe for you.

GrumpyCoder

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 37
    • Participated in NANY Challenge Five Times
    • Writer
Re: Ping with latency alarm?
« Reply #6 on: December 28, 2022, 07:45 PM »
I am pretty sure this could be classified as a NANY App this year I believe for you.

I created a NANY entry for it, thanks for the suggestion.  :Thmbsup:

Original address: https://www.donationcoder.com/forum/index.php?topic=51401.0