batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
jepoytengco
Posts: 8
Joined: 05 Jun 2016 21:17

batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#1 Post by jepoytengco » 05 Jun 2016 21:29

Hi, I'm new to this forum and not that good with batch files and scripting. I've made some research about continuously pinging an IP or domain with date/timestamp. The purpose of this is to compare with our ISP whenever they have downtime or intermittent connection. Below is the command I've used

ping -t <ip address>|cmd /q /v /c "(pause&pause)>nul & for /l %a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2 ip address>nul" >C:\filename.txt

This command works when I manually copied/paste this command on a command prompt, with or without admin rights

But when I tried this on a batch file just like the command below:

@echo off

ping -t <ip address>|cmd /q /v /c "(pause&pause)>nul & for /l %a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2 ip address>nul" >C:\filename.txt

it just shows filename.txt was unexpected at this time.

No errors shown on event viewer. Am I missing something here?

Thanks
Jeff

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#2 Post by Squashman » 06 Jun 2016 07:11

From the FOR command help file.
To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.

jepoytengco
Posts: 8
Joined: 05 Jun 2016 21:17

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#3 Post by jepoytengco » 06 Jun 2016 20:28

I tried to use double %%a on the command, it showed like this.

@echo off

ping -t <ip address>|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2 <ip address>>nul" > C:\<filename>.txt

so when I run this command, a command window opened up for a quick sec and disappeared.

Regards,
Jeff

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

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#4 Post by foxidrive » 07 Jun 2016 16:45

It would help to see the kind of output you are after from the ping command. Copy and paste a sample here.

I'd also curious to know if this has to be a one liner.

jepoytengco
Posts: 8
Joined: 05 Jun 2016 21:17

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#5 Post by jepoytengco » 07 Jun 2016 19:23

Here's a sample output of the command.

Wed 06/08/2016 9:19:33.22 Reply from <ip address being PING>: bytes=32 time=1ms TTL=254

As you can see on this output, this shows not only the latency but also the exact date and time. And this output is shown in a notepad/ txt document created in drive C

The purpose of this is to have a record since PING results has a limit, it will truncate the earlier results and it is hard for us to prove with our ISP if there's intermittent connection.

Thanks
Jeff

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

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#6 Post by foxidrive » 08 Jun 2016 07:47

Thanks.

These questions are to help me understand the requirement just a little more. I ask these due to past experience with similar questions and to avoid misunderstandings that have happened in the past.

Is the purpose of this to have a log for review after the ping command has terminated?

Does it all need to be in a single line of code?

jepoytengco
Posts: 8
Joined: 05 Jun 2016 21:17

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#7 Post by jepoytengco » 09 Jun 2016 20:19

Yes, the purpose of this is to have a record in a notepad file or text file to check previous timestamp not just the previous.

What do you mean by "Does it all need to be in a single line of code?"

Thanks
Jeff

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#8 Post by Compo » 10 Jun 2016 03:39

What's wrong with the logs in your router, (most routers even have a facility to select the information in those logs), and write them to file.

Additionally, why run a constant ping, can you not just run one every minute for instance.

Code: Select all

@Echo Off
:Loop
For /F "Skip=4 Tokens=*" %%a In (
   'Echo(Prompt $D$T$S^|(Cmd^&Ping -n 1 216.58.198.174^|Find "TTL"^)'
   ) Do Echo(%%a>>C:\Output.log
Timeout 60 1>Nul
GoTo :Loop

Single command: run it as a scheduled task every minute

Code: Select all

Echo(Prompt $D$T$S|(Cmd^&Ping -n 1 216.58.198.174|Find "TTL")|Find "TTL">>C:\Output.log

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#9 Post by Squashman » 10 Jun 2016 07:01

jepoytengco wrote: it will truncate the earlier results

Not understanding what you mean by this.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#10 Post by sambul35 » 10 Jun 2016 10:30

jepoytengco wrote:when I run this command, a command window opened up for a quick sec and disappeared.

To debug your code, open Cmd window first, then run the batch in it, post output here.

jepoytengco
Posts: 8
Joined: 05 Jun 2016 21:17

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#11 Post by jepoytengco » 10 Jun 2016 20:44

Squashman wrote:
jepoytengco wrote: it will truncate the earlier results

Not understanding what you mean by this.


What I mean here, it will just push the earlier messages upwards and you wont be able to see them anymore.

jepoytengco
Posts: 8
Joined: 05 Jun 2016 21:17

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#12 Post by jepoytengco » 10 Jun 2016 21:08

sambul35 wrote:
jepoytengco wrote:when I run this command, a command window opened up for a quick sec and disappeared.

To debug your code, open Cmd window first, then run the batch in it, post output here.


Wow!, guess what? Sambul has a point in this. I tried to opened up a command prompt first then run the <file>.bat then it worked. What am I missing here?

Thanks
Jeff

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#13 Post by Compo » 11 Jun 2016 04:41

The following single line in a batch file appears to be working for me!

Code: Select all

@Ping -t 216.58.198.174|Cmd /Q /V /C "(Pause&Pause)>Nul&For /L %%a In () Do (Set/P "Data="&&Echo(!Date! !Time! !Data!)&Ping -n 2 216.58.198.174>Nul">C:\Users\Compo\Desktop\Output.log
The cmd windows stays open.
This however doesn't!

Code: Select all

@Ping -t 216.58.198.174|Cmd /Q /V /C "(Pause&Pause)>Nul&For /L %%a In () Do (Set/P "Data="&&Echo(!Date! !Time! !Data!)&Ping -n 2 216.58.198.174>Nul">C:\Output.log
The cmd window closes immediately.

The difference… I cannot write to the root of C:\

jepoytengco
Posts: 8
Joined: 05 Jun 2016 21:17

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#14 Post by jepoytengco » 12 Jun 2016 21:06

I think I know what the problem is. I tried to change the path instead of drive C, I switched it to the desktop and it worked. I think there's a security issue here but I'm not sure whether it is a security issue or not.

Thanks
Jeff

jepoytengco
Posts: 8
Joined: 05 Jun 2016 21:17

Re: batch file to continuous PING multiple IP and domain with date/timestamp on a notepad file

#15 Post by jepoytengco » 12 Jun 2016 21:18

Its ok if it doesn't work on drive C, its ok if it shows output in my desktop, my next objective is if I type in the command this way:

Code: Select all

@echo off

ping -t <ip address>|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2

<ip address>>nul" > C:\users\jefferson.co\desktop\ping\<file1>.txt

ping -t <ip address>|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2

<ip address>>nul" > C:\users\jefferson.co\desktop\ping\<file2>.txt


It just shows the first command line, it doesn't runs the second file2. I'm guessing I need an "and" connector? so that it will run both commands.

Thanks
Jeff

Post Reply