0

I have the following PS code for logging the ping every second:

ping -t google.com | Foreach{"{0} - {1}" -f (Get-Date),$_} >> filename.txt

It indeed logs every second, that is, if the ping is 140ms, the next ping request is in 860ms, as I seemingly have data for every second without skips. However, when a timeout occurs, exactly 5 seconds are skipped every time:

12. 4. 2021 16:13:10 - Reply from 172.217.19.110: bytes=32 time=20ms TTL=111
12. 4. 2021 16:13:11 - Reply from 172.217.19.110: bytes=32 time=79ms TTL=111
12. 4. 2021 16:13:16 - Request timed out.
12. 4. 2021 16:13:17 - Reply from 172.217.19.110: bytes=32 time=26ms TTL=111
12. 4. 2021 16:13:18 - Reply from 172.217.19.110: bytes=32 time=67ms TTL=111

It is hard to believe that every time this happens, the connection is lost for exactly 5 seconds, than restored; it seems that the timing out "blocks" new ping commands. Is there a way to circumvent this?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Rolf
  • 331
  • 4
  • 13
  • 2
    Have you tried `ping -w 1000 -t …`? – aschipfl Apr 14 '21 at 11:47
  • 1
    [Default timeout](https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490968(v=technet.10)?redirectedfrom=MSDN) of ping is 4 seconds.Taking that into account, I think it's very feasible that you see 5 second gaps between pings that don't arrive (or return) before a next attempt. You can try setting the timeout to 1 second as @aschipfl mentioned. – Lieven Keersmaekers Apr 14 '21 at 12:00
  • @aschipfl Setting the timeout to 1000 sometimes causes it to "skip" the ping command for a second; 900ms fixes it. But my question would be whether it is possible to set say 5000ms timeout, but _still_ do a ping request every second? – Rolf Apr 28 '21 at 12:15
  • Well, then you would have to do simultaneous pings, because you would need to ping even while another one possibly times out, which is not possible with the `ping` command. – aschipfl Apr 28 '21 at 14:25
  • Anyway, I do not know how [`ping`](https://ss64.com/nt/ping.html) exactly does its timing; it seems that it really tries to do a ping attempt every second, because multiple response times appear not to contribute to the overall duration (open a Command Prompt window, type `prompt $D, $T$G$S`, then type `for %Z in (.) do ping -n 11 …` and check the time differences) upon successful attempts; however, as soon as ping attempts fail, an additional delay comes into account, which is the sum of all time-outs… – aschipfl Apr 28 '21 at 14:42
  • The actually used time-out seems to be given value (`-w`) rounded down to the closest multiple of 500 ms that is greater than or equal to 500 ms… – aschipfl Apr 28 '21 at 14:44
  • @aschipfl I suspected that the `ping` command doesn't do this, but I was wondering whether a cmd/powershell solution exists, by detaching the process (like '&' in unix) or something like that. – Rolf Apr 29 '21 at 15:04

2 Answers2

1

You're seeing the default packet request timeout. By default, Windows systems wait 4,000 milliseconds before the ping request times out. Add a bit of delay to bubble up to your script and there ya have it.

You can modify the timeout with the parameter /w

Ex. This will force the timeout to be 1 second. I think you'll see there is still a 1 second delay after the timeout interval before the response is delivered.

ping www.google.com /w 1000

Everything you ever wanted to know about ping on windows can be found on Microsoft's site by googling Microsoft Ping Command. It will be the result from learn.microsoft.com at or near the top of the list.

Ernest Correale
  • 451
  • 3
  • 5
  • Thanks for the explanation. As I wrote to another comment, this works with ~900ms, as then the previous ping request always finishes in <1 second, so the next one is never skipped. But my question would be whether it is possible to set say 5000ms timeout, but _still_ do a ping request every second? – Rolf Apr 28 '21 at 12:16
0

Powershell 7 has a -timeoutseconds parameter.

 test-connection -TimeoutSeconds 1 microsoft.com -count 1

   Destination: microsoft.com

Ping Source           Address                   Latency BufferSize Status
                                                   (ms)        (B)
---- ------           -------                   ------- ---------- ------
   1 DELL             *                               *          * TimedOut

Or you can make your own ping in .net and set a 100 millisecond timeout.

Function Get-Ping  {

  Param (
    [parameter(ValueFromPipeline)]
    [string[]]$Hostname='yahoo.com'
  )

  Begin {
    $Ping = New-Object System.Net.Networkinformation.ping
    $Timeout = 100 # ms
  }

  Process {
      $Ping.Send($hostname, $timeout)  | Add-Member -passthru hostname $hostname[0] | 
      select hostname,address,status,roundtriptime
  }

}


get-ping microsoft.com

hostname      Address   Status RoundtripTime
--------      -------   ------ -------------
microsoft.com         TimedOut             0
js2010
  • 23,033
  • 6
  • 64
  • 66