Batch file which pings and timestamps alongside

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
thamoomin
Posts: 3
Joined: 09 Sep 2013 05:48

Batch file which pings and timestamps alongside

#1 Post by thamoomin » 09 Sep 2013 05:59

Hi,

Wondering if anyone can help, I have the following code:

echo OFF
cd c:\users\rod\desktop
echo %time% >> ping.txt
ping ***.244.137.254 -t >> ping.txt


Above prints to the text file no problems in the following format:

12:36:15.25
Pinging ***.244.137.254 with 32 bytes of data:
Reply from ***.244.137.254: bytes=32 time=58ms TTL=248
Reply from ***.244.137.254: bytes=32 time=69ms TTL=248
Reply from ***.244.137.254: bytes=32 time=60ms TTL=248
Reply from ***.244.137.254: bytes=32 time=57ms TTL=248


but what I want is the timestamp alongside the ping so I can keep tracks on when a specific latency spike has taken place like so:

12:36:15.25

Pinging 135.244.137.254 with 32 bytes of data:
Reply from 135.244.137.254: bytes=32 time=58ms TTL=248 + timestamp
Reply from 135.244.137.254: bytes=32 time=69ms TTL=248 + timestamp
Reply from 135.244.137.254: bytes=32 time=60ms TTL=248 + timestamp
Reply from 135.244.137.254: bytes=32 time=57ms TTL=248 + timestamp


Is the above possible? I have tried numerous things with no results.

Any help appreciated.

Regards,
Ross

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file which pings and timestamps alongside

#2 Post by foxidrive » 09 Sep 2013 06:17

Code: Select all

@echo off
set "file=c:\users\rod\desktop\ping.txt"
set ip=www.google.com

setlocal enabledelayedexpansion
:loop
for /f "delims=" %%a in ('ping %ip% -n 1 ^|find "TTL" ') do >> "%file%" echo %%a - !date! @ !time!&echo %%a
goto :loop

thamoomin
Posts: 3
Joined: 09 Sep 2013 05:48

Re: Batch file which pings and timestamps alongside

#3 Post by thamoomin » 10 Sep 2013 02:00

foxidrive wrote:

Code: Select all

@echo off
set "file=c:\users\rod\desktop\ping.txt"
set ip=www.google.com

setlocal enabledelayedexpansion
:loop
for /f "delims=" %%a in ('ping %ip% -n 1 ^|find "TTL" ') do >> "%file%" echo %%a - !date! @ !time!&echo %%a
goto :loop


Thanks foxidrive, this was exactly what I was looking for :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file which pings and timestamps alongside

#4 Post by foxidrive » 10 Sep 2013 02:24

Thanks. Just be aware that this reports successful pings only.

If you also want time outs then change the

find "TTL"

to this and it should work.

find /i "reply from"

thamoomin
Posts: 3
Joined: 09 Sep 2013 05:48

Re: Batch file which pings and timestamps alongside

#5 Post by thamoomin » 10 Sep 2013 09:25

foxidrive wrote:Thanks. Just be aware that this reports successful pings only.

If you also want time outs then change the

find "TTL"

to this and it should work.

find /i "reply from"


Thanks, did notice that and changed it to reply, couple of quick questions, been trying to understand the code, one bit I dont understand is:

^|find

I understand | is a pipe/redirect but what is the meaning of the ^ before it? Could find nothing online.

Regards,
Ross

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch file which pings and timestamps alongside

#6 Post by ShadowThief » 10 Sep 2013 18:13

It's an escape character. You need it so that the pipe is included in the thing the for loop is doing instead of everything after it being separate.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file which pings and timestamps alongside

#7 Post by foxidrive » 10 Sep 2013 19:08

Just adding here that without escaping regular pipes and redirection characters in a for in do loop command, they will generate errors.

Post Reply