1

Want to ping specific host with arbitrary number of ICMP requests, but also want to ping program terminate on first reply (speed-up script). Built-in windows ping and few other I tested have not such feature. Anyone known windows ping program with this ability? I know I can loop ping program with "-n 1" parameter, but this cause to ping program is started each time loop running and in case of massive usage could cause unwanted system load.

bgtvfr
  • 1,262
  • 10
  • 20
user2956477
  • 121
  • 3

2 Answers2

0

You can do something like this in PowerShell, which as the Test-Connection cmdlet as a wrapper for ping.

 param(
     [string]$Name = "8.8.8.8",
     [int]$count = 4
 )

 [bool]$Success = $false

 For ($i=0; $i -le $count; $i++) {

     $result = Test-Connection -ComputerName $Name -Count 1 -ErrorAction SilentlyContinue
     if ($result.StatusCode -eq 0)
     {
          $Success = $true
          $result 
          break;
     }
 }

 Write-Output "Successful connection: $Success"

Test-Connection always uses a single ping, and I put a loop around it, as soon as a ping is successful, we are breaking out of the loop, ideally after the first ping, Otherwise it try x times, as specified in $count.

PowerShell is not starting the ping.exe executable, it uses the network APIs underneath, so it may perform a bit better under load.

Peter Hahndorf
  • 14,058
  • 3
  • 41
  • 58
  • Well and how is it with powershell interpreter itself start load? cmd.exe and conhost.exe start takes very minimal system resources. I am talking about system which monitor hundreds targets every minute and machine is not dedicated. – user2956477 Aug 14 '17 at 14:27
  • The PowerShell process itself is indeed much heavier than cmd.exe and the start-up will take longer. But depending on your requirements you may be able to monitor all targets with a single script. – Peter Hahndorf Aug 15 '17 at 01:12
0

Check the -n parameter

-n Count : Specifies the number of Echo Request messages sent. The default is 4.

ping.exe -n 1 should send only one Echo Request message

If you are looking to speed up things you might want to check the other parameters as well, specifically the timeout parameter.

Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39