How can I perform a ping every X minutes and check the response time?
Solution 1
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.
Solution 2
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:
- you can replace
xxx.xxx.xxx.xxx
withgoogle.com
- to edit the interval change the
3000
to60
(for 1 minutes) or10
(for 10 seconds) - if you need to put this command in batch file (.bat or .cmd), then make sure you replace
%
with%%
Solution 3
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.
Solution 4
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..
Solution 5
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.
Related videos on Youtube
Comments
-
Waza_Be over 1 year
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
-
Admin over 12 years@Zoredache I cannot install such softwares on a Process control computer: dev.pulsed.net/wp/?p=31
-
Admin over 12 years"win XP Professional" is not DOS.
-
-
Waza_Be over 12 yearstimeout is not recognized as an internal or external command
-
mekwall over 12 years@Profete162, do you have the sleep command then? If so, just replace
timeout
withsleep
. What version of Windows are you doing this? -
Waza_Be over 12 yearsSame for sleep... I am using win XP Professional
-
mekwall over 12 years@Profete162, old school! :) I'll add a secondary method which you can use when those commands are missing.
-
barlop about 9 yearsmaybe in cygwin.. cygwin lets you use bash in windows
-
THE JOATMON almost 9 years-w just specifies how long it will wait for a reply, not how long it will wait to ping.
-
Ben N about 8 yearsThe question asked how to write the information to a file. Can you edit your answer to accomplish that?
-
DavidPostill almost 8 yearsWelcome 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?
-
wersimmon over 7 yearsOn Windows,
-w
specifies the maximum time to wait before considering a ping "lost", not the time between pings. -
Máté Juhász over 4 yearsThere are already several answers to this question, please also explain how your answer is better /different.
-
Jovylle Bermudez almost 4 yearsWhy is there need a file?
-
mekwall almost 4 years@JovylleBermudez No need, but it was in the example in the question.