41

I am currently working in a big company and we have serious latency issues. This is happening in a process control system, and is unacceptable (Open a valve sometimes take 2 minutes before command start)

I want to double-check when the network team says "everything is alright on the network". So, I want to create a loop that pings the server and writes the result in a text file.

I am not a batch expert, but do you think this code is correct to use?

@ECHO OFF

:LOOPSTART

time /T
ping xxx.xx.x.x  -t >> filename.txt
sleep -m 3000

GOTO LOOPSTART
3

12 Answers 12

43

Looks fine to me, but there's no need to loop it if you want to continuously ping the IP. Then you could simply do it like this:

@ECHO OFF
set IPADDRESS=x.x.x.x
ping %IPADDRESS% -t >> filename.txt

If you want to ping every X minute, use the loop:

@ECHO OFF
set IPADDRESS=x.x.x.x
set INTERVAL=60
:PINGINTERVAL
ping %IPADDRESS% -n 1 >> filename.txt
timeout %INTERVAL%
GOTO PINGINTERVAL

As you can see I replaced the sleep command with timeout. That's because sleep isn't always available on some systems whereas timeout usually is.

Missing sleep or timeout commands on your system? Don't fret. Just replace timeout with the following hack:

@ping 127.0.0.1 -n %INTERVAL% > nul

This hack simply pings your local address, and since it will respond instantly we can use this to emulate a delay in execution.

7
  • timeout is not recognized as an internal or external command
    – Waza_Be
    Oct 11, 2011 at 8:02
  • @Profete162, do you have the sleep command then? If so, just replace timeout with sleep. What version of Windows are you doing this?
    – mekwall
    Oct 11, 2011 at 8:14
  • Same for sleep... I am using win XP Professional
    – Waza_Be
    Oct 11, 2011 at 8:21
  • 1
    @Profete162, old school! :) I'll add a secondary method which you can use when those commands are missing.
    – mekwall
    Oct 11, 2011 at 8:22
  • Why is there need a file?
    – Jovylle
    Jul 18, 2020 at 2:08
10

I know it's a windows question (and an old one at that), but maybe it's similar to Linux and OSX. This is the first thing that came up when I was looking for a simple command to keep network traffic on my laptop. Might be useful to someone looking for something similar.

in a bash script:

WAITSECONDS=30 #or whatever your needs are
IPTOPING=8.8.8.8 #or whatever your needs are
ping -i ${WAITSECONDS} ${IPTOPING} > logfile

Single line ex pinging google dns every 30sec:

ping -i 30 8.8.8.8 > logfile

Works in OSX and Linux, should be pretty standard though, don't know what system you're on.

1
  • 2
    maybe in cygwin.. cygwin lets you use bash in windows
    – barlop
    Apr 27, 2015 at 18:03
4

For a one-liner solution use the following:

cmd /v /c "(for /l %a in () do @for /f "tokens=*" %b in ('ping -w 1000 -n 1 xxx.xxx.xxx.xxx ^| findstr "Reply Request Unknown Destination"') do @echo !DATE! !TIME! %b & timeout 3000 >NUL) > pingtestresults.txt"

NB:

  1. you can replace xxx.xxx.xxx.xxx with google.com
  2. to edit the interval change the 3000 to 60 (for 1 minutes) or 10 (for 10 seconds)
  3. if you need to put this command in batch file (.bat or .cmd), then make sure you replace % with %%
3

Thanks to WSL2, this is now an option:

wsl -- ping -i <SECONDS> <ADDRESS>

Examples:

wsl -- ping -i 600 1.1.1.1
wsl -- ping -i 600 1.1.1.1 >> ping.log
wsl -d Ubuntu -- ping -i 10 1.1.1.1 >> ping.log

This may or may not work properly in WSL 1, as low-level network tools were limited in WSL 1. Since this is the Linux ping, it will support all Linux ping features supported by WSL2.

WSL2 needs to be installed, of course.

2

If you want to just paste it into a command window on windows...

(for /l %a in () do @for /f "tokens=*" %b in ('ping -w 1000 -n 1 8.8.8.8 ^| findstr "Reply Request Unknown Destination"') do @echo %b & timeout 3 >NUL)

It ping's every 3 seconds... until you stop it

This is better because you are not needing to write to a log file, (why would you really need a log file) just to the immediate window and it gives you the desired results "immediately" :)

If for some reason you can also pipe out to log-file by doing this: (for /l %a in () do @for /f "tokens=*" %b in ('ping -w 1000 -n 1 8.8.8.8 ^| findstr "Reply Request Unknown Destination"') do @echo %b & timeout 3 >NUL)>file.txt

Also, you can adjust the timeout by changing the value after 'timeout' as it is in this case 3 seconds...

And you don't have to save to a batch file... Just copy and paste this text string from this text stored in a saved cloud location.. or folder of commands you like to use.. etc..

1
  • There are already several answers to this question, please also explain how your answer is better /different. Oct 30, 2019 at 5:21
1

the timeout switch is not the same as what you are looking for as a 'wait' switch. The timeout switch with Windows ping command simply tells the command window how long to wait before RECEIVING the reply, not how long to wait before sending the NEXT reply.

1
1

The question is not quite clear. Apparently there is no use in continuous loop in a batch file appending to a file. Especially when this batch file is started by a repeating external event... unless you want to fill up the disk space... or to initiate tons of command prompts and choke the system...

My assumption is there should be some time period for testing and then the batch should end. I also assume that today most of the MS Windows are equipped with Powershell, which might become useful for this case. Here is the twoliner:

powershell "get-date | out-file <log filename>"
powershell "test-connection <IP> -delay <interval> -count <how many pings> | out-file -append <log filename>"

This can be put in a batch file, or run as a powershell script, in this case remove 'powershell' and the double quotes.

Still, bash on Windows seems to be the best option...

0

expanding on the answer by Sparks above, I put his one line code in a batch file with some modifications.

cmd /v /c "(for /l %%a in () do @for /f "tokens=*" %%b in ('ping -w 1000 -n 1 %1 ^| 
findstr "Reply Request Unknown Destination"') do @echo !DATE! !TIME! %%b & timeout %2 
>NUL) > pingtestresults.txt"

saved as PingD.bat use the following to execute PingD [machine name/IP] [delay between pings in seconds]

e.g. PingD MyDC01 10 will send a ping to MyDC01 every 10 seconds.

0

I needed to measure how stable my internet connection was, and didn't want to ping every second. So I wrote this batch file to keep track of the ping stats, while waiting longer between each ping. While timeout is waiting x seconds, you can press any key (e.g., spacebar) to immediately loop.

@echo off
if "%1"=="" goto help

setlocal ENABLEDELAYEDEXPANSION

set ip=%1
set timeout=%2
set cnt=0
set cntLost=0
set total=0
set min=100000
set max=0

:loop
for /f "tokens=4 delims==" %%i in ('ping -n 1 !ip!^|findstr "100% Average"') do (
  set /a cnt+=1
  set ms=%%i
  set ms=!ms:~0,-2!
  set ms=!ms: =!
  set loss=!ms:~-4!
  if "!loss!" equ "loss" (set lost=1) else (set lost=0)
  if !lost! equ 1 (
    set /a cntLost+=1
    set ms=--
  ) else (
    set /a total+=!ms!
    set /a avg=!total!/!cnt!
    if !ms! lss !min! set min=!ms!
    if !ms! gtr !max! set max=!ms!
  )
  set /a pLost=!cntLost! * 100 / !cnt!
  set msg=time=!ms!ms; Min=!min!ms, Max=!max!ms, Avg=!avg!ms, Total=!cnt!, Lost=!cntLost! ^(!pLost!%%^)
  title !msg!
  echo Reply from !ip!: !msg!
)
timeout /t !timeout!>nul
goto loop

:help
echo.
echo %0
echo.
echo Usage: %0 ip_address wait_between_pings
echo.

:TheEnd
endlocal

Here is how the output looks.

Reply from x.x.x.x: time=17ms; Min=17ms, Max=17ms, Avg=17ms, Total=1, Lost=0 (0%)
Reply from x.x.x.x: time=15ms; Min=15ms, Max=17ms, Avg=16ms, Total=2, Lost=0 (0%)
Reply from x.x.x.x: time=16ms; Min=15ms, Max=17ms, Avg=16ms, Total=3, Lost=0 (0%)
Reply from x.x.x.x: time=15ms; Min=15ms, Max=17ms, Avg=15ms, Total=4, Lost=0 (0%)
Reply from x.x.x.x: time=10ms; Min=10ms, Max=17ms, Avg=14ms, Total=5, Lost=0 (0%)
Reply from x.x.x.x: time=12ms; Min=10ms, Max=17ms, Avg=14ms, Total=6, Lost=0 (0%)
Reply from x.x.x.x: time=18ms; Min=10ms, Max=18ms, Avg=14ms, Total=7, Lost=0 (0%)
Reply from x.x.x.x: time=12ms; Min=10ms, Max=18ms, Avg=14ms, Total=8, Lost=0 (0%)
Reply from x.x.x.x: time=14ms; Min=10ms, Max=18ms, Avg=14ms, Total=9, Lost=0 (0%)

Now I can see cumulative stats, just like a continuous ping, but still wait a variable number of seconds between each ping.

0

This batch file does not show every ping.
It only writes a line if a ping took longer than %MaxTimeDif% milliseconds.

If you want to see every pingtime, just set "OnlyShowTimeouts=0"

pingtime.bat

@echo off
:: pingtime.bat
:: Cyclic ping of a server and show the timestamp if the ping needs longer than a specific MaxTime
:: Michael Hutter, 28.03.2024

set HostToPing=MyHost.de
:: MaxTimeDif in Milliseconds
set MaxTimeDif=500
set OnlyShowTimeouts=1

setlocal

echo ping %HostToPing% ...
:start
set TimestampBefore=%time%
ping %HostToPing% -n 1 > nul

:: Konvertiere Zeitstempel in ein von PowerShell verarbeitbares Format
for /f "tokens=1-4 delims=:.," %%a in ("%TimestampBefore%") do set "time1=%%a:%%b:%%c.%%d"
for /f "tokens=1-4 delims=:.," %%a in ("%time%") do set "time2=%%a:%%b:%%c.%%d"

:: Berechne Zeitdifferenz in Millisekunden mit PowerShell
for /f "usebackq tokens=2 delims=:" %%a in (`powershell -Command "[datetime]::Parse('%time2%') - [datetime]::Parse('%time1%')" ^| find "TotalMilliseconds"`) do set "timediff=%%a"

:: Entferne Leerzeichen vom Anfang der Zeitdifferenz
set "timediff=%timediff: =%"

:: Ausgabe der Zeitdifferenz in Millisekunden
if %timediff% gtr %MaxTimeDif% (
    echo %date% %time%: Warnung: ping %HostToPing% - Zeitdifferenz=%timediff%ms ^> %MaxTimeDif%ms
    rem exit /b 1
) else if not "%OnlyShowTimeouts%" == "1" (
    echo %time% Zeitdifferenz: %timediff% Millisekunden
)

endlocal

timeout 1 > nul
goto start

And the output looks like this:

C:\>pingtime.bat
ping MyHost.de ...
28.03.2024 12:08:37,80: Warnung: MyHost.de - Zeitdifferenz=720ms > 500ms
28.03.2024 12:08:55,79: Warnung: MyHost.de - Zeitdifferenz=810ms > 500ms
28.03.2024 12:09:04,37: Warnung: MyHost.de - Zeitdifferenz=3690ms > 500ms
28.03.2024 12:09:20,79: Warnung: MyHost.de - Zeitdifferenz=610ms > 500ms
28.03.2024 12:09:22,71: Warnung: MyHost.de - Zeitdifferenz=910ms > 500ms
-2

On Windows based systems we can use the following command to ping a server after specific interval

ping xxx.xxx.xxx.xxx -w xxxx -n xx >> c:\logfile.txt

where -w specifies intervals in milliseconds so 1000 ~ 1 second => 3000 for 3 sec delay -n specifies number of times ping will be sending query to the server at xxx.xxx.xxx.xxx .

1
  • 16
    -w just specifies how long it will wait for a reply, not how long it will wait to ping. May 20, 2015 at 15:04
-2

This is the windows command I use to ping a specific IP at a specified interval (10 seconds in this example):

ping -t <ip.address> -w 10000

-t says ping continuously.

-w says wait this long before next ping. Here's a 1 minute wait example:

ping -t <ip.address> -w 60000

Enjoy, and Good Luck!!

2
  • The question asked how to write the information to a file. Can you edit your answer to accomplish that?
    – Ben N
    Mar 23, 2016 at 15:36
  • 19
    On Windows, -w specifies the maximum time to wait before considering a ping "lost", not the time between pings.
    – wersimmon
    Sep 7, 2016 at 20:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .