Save Ping Output in a text file
I often have to ping servers for connectivity status. Is there a way to save the ping results (output) in a text file so that I can save entire day's ping results in a text file.
I am using Windows XP SP3.
Ping example:
ping 192.168.1.1 -t
(using windows' ping)
or
ping 192.168.1.1
(using cygwin)
Use redirection, for example:
ping 192.168.1.1 -t > filename.txt
This will redirect all (standard) output from the program into filename.txt
, which will be created if it doesn't exist and overwritten if it does.
You can use >>
instead of >
to redirect the output to a file and append the results to the end of the file, instead of overwriting (with thanks to @Jane T for the reminder).
Note that you will not receive the normal on-screen output if you do this.
Update in response to comment
To delay between pings and record the time of each, you can do some scripting.
Here is a quick Windows batch file I've thrown together. It prints the time, pings Google, then waits for 3 seconds before repeating itself. I'm not a batch file expert so if anyone spots any problems please flag them up! And this probably isn't the "best" way to achieve what you are after - that might make for a separate question really.
@ECHO OFF
:LOOPSTART
time /T
ping www.google.com -n 4
sleep -m 3000
GOTO LOOPSTART
Save this in a .bat
file somewhere, edit the ping target and delay time as you need it, then run the .bat
using redirection to pump the output of the whole thing to a file. You may need to replace the sleep -m 3000
command with timeout /T 3
depending on your Windows version.
Note that this batch file never ends, but can be terminated by Ctrl + C and then Y if run from cmd
. (You must press Y because it asks if you want to stop the batch file - even though you cannot see the question because you've redirected the output!)
If you are using the command prompt just redirect it to a text file using this format
ping 192.168.1.1 > ping.txt
That will do it.
You can use:
> ping 192.168.1.1 -t > ping-results
I wrote the script that pings google.com every 5 seconds and logging results with current time. Here you can find output to variables "commandLineStr" (with indices)
@echo off
:LOOPSTART
echo %DATE:~0% %TIME:~0,8% >> Pingtest.log
SETLOCAL ENABLEDELAYEDEXPANSION
SET scriptCount=1
FOR /F "tokens=* USEBACKQ" %%F IN (`ping google.com -n 1`) DO (
SET commandLineStr!scriptCount!=%%F
SET /a scriptCount=!scriptCount!+1
)
@ECHO %commandLineStr1% >> PingTest.log
@ECHO %commandLineStr2% >> PingTest.log
ENDLOCAL
timeout 5 > nul
GOTO LOOPSTART
Also if you want to see the ping results in display you can use this code
@ECHO OFF
:LOOPSTART
date /T >>Pingtest.log
time /T >>Pingtest.log
REM this line show you the ping results in display
ping 8.8.8.8 -n 1
REM this line print the ping results in the log file
ping 8.8.8.8 -n 10 >>PingTest.log
sleep -m 1000
GOTO LOOPSTART