How can I perform a ping every X minutes and check the response time?

26

5

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

Waza_Be

Posted 2011-10-11T07:02:21.357

Reputation: 395

http://oss.oetiker.ch/smokeping/ – Zoredache – 2011-10-11T07:43:04.370

@Zoredache I cannot install such softwares on a Process control computer: http://dev.pulsed.net/wp/?p=31

– Waza_Be – 2011-10-11T08:06:16.180

"win XP Professional" is not DOS. – user1686 – 2011-10-11T12:32:24.210

Answers

32

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.

mekwall

Posted 2011-10-11T07:02:21.357

Reputation: 1 599

timeout is not recognized as an internal or external command – Waza_Be – 2011-10-11T08:02:20.773

@Profete162, do you have the sleep command then? If so, just replace timeout with sleep. What version of Windows are you doing this? – mekwall – 2011-10-11T08:14:24.173

Same for sleep... I am using win XP Professional – Waza_Be – 2011-10-11T08:21:24.273

1@Profete162, old school! :) I'll add a secondary method which you can use when those commands are missing. – mekwall – 2011-10-11T08:22:10.343

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

sparks

Posted 2011-10-11T07:02:21.357

Reputation: 133

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!

Sean Vikoren

Posted 2011-10-11T07:02:21.357

Reputation: 350

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.

james

Posted 2011-10-11T07:02:21.357

Reputation: 1

1

Welcome to Super User! This is really a comment and not an answer to the original question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. Please read Why do I need 50 reputation to comment? What can I do instead?

– DavidPostill – 2016-06-24T17:04:09.347

0

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

dmitry

Posted 2011-10-11T07:02:21.357

Reputation: 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.

James Tew

Posted 2011-10-11T07:02:21.357

Reputation: 1

0

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

CA Martin

Posted 2011-10-11T07:02:21.357

Reputation: 1

There are already several answers to this question, please also explain how your answer is better /different. – Máté Juhász – 2019-10-30T05:21:39.320

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.

Robert Heath

Posted 2011-10-11T07:02:21.357

Reputation: 1

1maybe in cygwin.. cygwin lets you use bash in windows – barlop – 2015-04-27T18:03:23.677

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

Software_Programineer

Posted 2011-10-11T07:02:21.357

Reputation: 125

The question asked how to write the information to a file. Can you edit your answer to accomplish that? – Ben N – 2016-03-23T15:36:36.307

16On Windows, -w specifies the maximum time to wait before considering a ping "lost", not the time between pings. – wersimmon – 2016-09-07T20:56:18.270

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

Praveer Verma

Posted 2011-10-11T07:02:21.357

Reputation: 7

15-w just specifies how long it will wait for a reply, not how long it will wait to ping. – Devil's Advocate – 2015-05-20T15:04:57.643