31

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

37

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.

6
  • timeout is not recognized as an internal or external command – Waza_Be Oct 11 '11 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 '11 at 8:14
  • Same for sleep... I am using win XP Professional – Waza_Be Oct 11 '11 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 '11 at 8:22
  • Why is there need a file? – Jovylle Bermudez Jul 18 '20 at 2:08
3

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 %%
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...

1

For a Windows solution, if you are willing to install wtee.exe, the following will work (pingloop.bat):

@echo off
SETLOCAL

set pingTarget=yahoo.com
set fileName=pinglog.txt
set waitSeconds=420

echo. | wtee -a %fileName%
echo ====================================================== | wtee -a %fileName%
echo Starting Ping Loop; Logging to %fileName% | wtee -a %fileName%

:timestamp
set day=%date:~7,2%
set month=%date:~4,2%
set year=%date:~10,4%

set timestamp=%year%-%month%-%day%_%time%
echo. | wtee -a %fileName%
echo ------------------------------------------------------ | wtee -a %fileName%
echo Ping Timestamp: %timestamp% | wtee -a %fileName%

:ping
ping %pingTarget% -n 1 | wtee -a %fileName%

:wait
ping 127.0.0.1 -n %waitSeconds% > nul

goto timestamp

ENDLOCAL

Cheers!

1

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. – Máté Juhász Oct 30 '19 at 5:21
0

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
  • 1
    maybe in cygwin.. cygwin lets you use bash in windows – barlop Apr 27 '15 at 18:03
0

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
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

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.

-1

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. – JOATMON May 20 '15 at 15:04
-1

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 '16 at 15:36
  • 17
    On Windows, -w specifies the maximum time to wait before considering a ping "lost", not the time between pings. – wersimmon Sep 7 '16 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.