Ping IP address with time stamp and log to txt file.

Hello,

I am troubleshooting a suspect router which is dropping connection.

I would like to ping the router (from inside the network) and log this in a text file with time stamps of each ping. Then I will cross refernce the ping drops with router logs.

I've done alot of reading but cannot seem to get the desired output. Could someone advise please?

thanks

Liam
LVL 1
unrealone1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Describe your desired output??

ping -t ip-adress-or-name >> [path\]logfile.extension

Open in new window

0
Wilder_AdminCommented:
You can also use the free version of http://www.pingplotter.com/ There you have additionally a nice view of your meassured data.
0
Steve KnightIT ConsultancyCommented:
I have got a few scripts on my scripts site below, search for "PING", e.g.

http://scripts.dragon-it.co.uk/links/batch-ping-list-of-hosts

Have just updated one of those to date/time stamp for you:

@echo off
SETLOCAL enabledelayedexpansion
REM results from nslookup of cluster8out.eu.messagelabs.com

Set list=85.158.140.179, 85.158.140.211, 85.158.137.99, 85.158.139.35, 85.158.140.179
set list=%list%, 85.158.140.211,, 85.158.138.131, 85.158.143.51, 85.158.38.147
set list=%list%, 85.158.140.195, 85.158.139.19, 85.158.139.179, 85.158.139.163
set list=%list%, 1.1.1.1,failed.tofind.com


REM location for script log file
set log="logfile.txt"

:Start

for %%a in (%list%) do (

REM This waits upto 2 seconds for a PING response, adjust as wanted
ping -a -n 1 -w 2000 %%a > pingres.txt
set OK=!errorlevel!

for /f "tokens=2 delims= " %%N in ('find /i "Pinging" ^< pingres.txt') do set name=%%N
   if NOT [!ok!]==[0]  (
      set /p p=%date% %time% FAIL to %%a ... <nul
      type pingres.txt | find /v ":" | find "." 

      (set /p p=%date% %time% FAIL to %%a ... <nul
      type pingres.txt | find /v ":" | find "." )>>%log%
   ) ELSE (
      for /f "tokens=7 delims=^<= " %%T in ('find "Reply" ^< pingres.txt') do set pingtime=%%T
      echo %date% %time% OK to %%a [!pingtime!] - !name!
      echo %date% %time% OK to %%a [!pingtime!] - !name! >>%log%
   )
)

PING 127.0.0.01 -n 1 -w 5000 > NUL
Goto Start

Open in new window


which gives you output like this to screen and logfile.  If you just want the fails then can remove the bits that log the "OK".  You can soon have another script send you logfile.txt by email every now and again etc.

13/10/2014 11:25:55.90 OK to 85.158.140.179 [17ms] - mail205.messagelabs.com
13/10/2014 11:25:55.90 OK to 85.158.140.211 [17ms] - mail194.messagelabs.com
13/10/2014 11:25:55.90 OK to 85.158.137.99 [33ms] - mail217.messagelabs.com
13/10/2014 11:25:55.90 OK to 85.158.139.35 [22ms] - mail179.messagelabs.com
13/10/2014 11:25:55.90 OK to 85.158.140.179 [16ms] - mail205.messagelabs.com
13/10/2014 11:25:55.90 OK to 85.158.140.211 [16ms] - mail194.messagelabs.com
13/10/2014 11:25:55.90 OK to 85.158.138.131 [33ms] - mail186.messagelabs.com
13/10/2014 11:25:55.90 OK to 85.158.143.51 [17ms] - mail204.messagelabs.com
13/10/2014 11:25:55.90 FAIL to 85.158.38.147 ... Request timed out.
13/10/2014 11:25:55.90 OK to 85.158.140.195 [18ms] - mail193.messagelabs.com
13/10/2014 11:25:55.90 OK to 85.158.139.19 [30ms] - mail178.messagelabs.com
13/10/2014 11:25:55.90 OK to 85.158.139.179 [22ms] - mail189.messagelabs.com
13/10/2014 11:25:55.90 OK to 85.158.139.163 [21ms] - mail188.messagelabs.com
13/10/2014 11:25:55.90 FAIL to 1.1.1.1 ... Request timed out.
13/10/2014 11:25:55.90 FAIL to failed.tofind.com ... Request timed out.

Open in new window

0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

unrealone1Author Commented:
Hi,

Desired output is:

13/10/2014:11:28 Ping 192.168.xx.xxx Reply from 192.168.xx.xxx time<1ms
0
Steve KnightIT ConsultancyCommented:
Well my script is pretty close to that for you... but here goes:

@echo off
SETLOCAL enabledelayedexpansion
REM results from nslookup of cluster8out.eu.messagelabs.com

Set list=85.158.140.179, 85.158.140.211, 85.158.137.99, 85.158.139.35, 85.158.140.179
set list=%list%, 85.158.140.211,, 85.158.138.131, 85.158.143.51, 85.158.38.147
set list=%list%, 85.158.140.195, 85.158.139.19, 85.158.139.179, 85.158.139.163
set list=%list%, 1.1.1.1,failed.tofind.com


REM location for script log file
set log="logfile.txt"

:Start

for %%a in (%list%) do call :pingit %%a
PING 127.0.0.01 -n 1 -w 5000 > NUL
Goto Start


:pingit

REM This waits upto 2 seconds for a PING response, adjust as wanted and gets the third line of the PING into result
for /f "skip=2 tokens=*" %%r in ('ping -a -n 1 -w 2000 %1') do set result=%%r & goto next
:next

echo %date% %time:~0,5% %1 ... %result%
echo %date% %time:~0,5% %1 ... %result%>>%log%


exit /b

Open in new window


e.g.

3/10/2014 12:19 85.158.140.179 ... Reply from 85.158.140.179: bytes=32 time=16ms TTL=122
3/10/2014 12:19 85.158.140.211 ... Reply from 85.158.140.211: bytes=32 time=16ms TTL=122
3/10/2014 12:19 85.158.138.131 ... Reply from 85.158.138.131: bytes=32 time=31ms TTL=121
3/10/2014 12:19 85.158.143.51 ... Reply from 85.158.143.51: bytes=32 time=17ms TTL=122
3/10/2014 12:19 85.158.38.147 ... Request timed out.
3/10/2014 12:19 85.158.140.195 ... Reply from 85.158.140.195: bytes=32 time=16ms TTL=122
3/10/2014 12:19 85.158.139.19 ... Reply from 85.158.139.19: bytes=32 time=22ms TTL=122
3/10/2014 12:19 85.158.139.179 ... Reply from 85.158.139.179: bytes=32 time=21ms TTL=122
3/10/2014 12:19 85.158.139.163 ... Reply from 85.158.139.163: bytes=32 time=23ms TTL=122
3/10/2014 12:19 1.1.1.1 ... Request timed out.
3/10/2014 12:19 failed.tofind.com ... Request timed out.
3/10/2014 12:19 85.158.140.179 ... Reply from 85.158.140.179: bytes=32 time=16ms TTL=122
3/10/2014 12:19 85.158.140.211 ... Reply from 85.158.140.211: bytes=32 time=16ms TTL=122

Open in new window

0
akbCommented:
As Wilder_Admin said, pingplotter is the way to go
0
Steve KnightIT ConsultancyCommented:
only reason I tend to use a script is it is often something I need to leave running a while in background on customer system and not wanting to put third party software on, but yes is good when you can!
0
unrealone1Author Commented:
Hi,

Thanks for you comments. I agree with dragon, in this instance i would rather not install software.

Dragon where can I modify the script to change to a single address to ping?

Liam
0
Steve KnightIT ConsultancyCommented:
if you just want a single address just replace these lines:

Set list=85.158.140.179, 85.158.140.211, 85.158.137.99, 85.158.139.35, 85.158.140.179
set list=%list%, 85.158.140.211,85.158.138.131, 85.158.143.51, 85.158.38.147
set list=%list%, 85.158.140.195, 85.158.139.19, 85.158.139.179, 85.158.139.163
set list=%list%, 1.1.1.1,failed.tofind.com

Open in new window


with:

set list=x.x.x.x

Open in new window


then the rest can stay the same and if you want to add any other IP's then just add a comma and other IP's to the line.  Would suggest maybe a couple of IP's though, e.g.:

Another PC / server locally (to show LAN is OK), router internal IP, router external IP, an IP on the net...

Steve
0
ste5anSenior DeveloperCommented:
btw, when MSDOS means Windows, I would prefer PowerShell and the Test-Connection cmdlet.
0
unrealone1Author Commented:
Dragon, I've made that change and cannot see where it is sending the logs too?


REM location for script log file
set log="logfile.txt"

Changed this to c:\logfile.txt but couldnt see one generated? Do i need to create this manually?
0
Steve KnightIT ConsultancyCommented:
Well as it stands above it made a log file OK for me.  Without a path it will go into the same dir as the script if run from a command prompt, or wherever your shortcut ends up putting it.  If you run it from a network drive \\server\share etc. it would end up going into c:\windows\system32 by default.  I have added showing the directory name where the log is to help:

Also yes you can specify a dir like c:\logfile.txt but generally you won't be able to save to that directory even if you are an admin with UAC turned on so try c:\scripts\logfile.txt or similar?

Steve

@echo off
SETLOCAL enabledelayedexpansion
REM results from nslookup of cluster8out.eu.messagelabs.com

Set list=85.158.140.179, 85.158.140.211, 85.158.137.99

REM location for script log file
set log="logfile.txt"
echo Current log file is in %cd%

:Start

for %%a in (%list%) do call :pingit %%a
PING 127.0.0.01 -n 1 -w 5000 > NUL
Goto Start


:pingit

REM This waits upto 2 seconds for a PING response, adjust as wanted and gets the third line of the PING into result
for /f "skip=2 tokens=*" %%r in ('ping -a -n 1 -w 2000 %1') do set result=%%r & goto next
:next

echo %date% %time:~0,5% %1 ... %result%
echo %date% %time:~0,5% %1 ... %result%>>%log%


exit /b

Open in new window

0
unrealone1Author Commented:
Hi Steve,

Still struggling with this. All i get is

"Current log file is in c:\ping testing"
0
Steve KnightIT ConsultancyCommented:
Odd.  Exactly as the script is above I get a logfile.txt in the current dir.

can you start a cmd.exe prompt
cd /d c:\wherever the script is
scriptfile.cmd

then try removing @echo off from top of file and do it again.
Does it show you then?

If no obvious please try with above script without changing anything, any good?  What OS are you running on?
0
unrealone1Author Commented:
Renamed the extension and it seemed to work. Then when i changed the ping address it said:

The system cannot find the file specified.

But then on some lines it does ping?

Liam
0
Steve KnightIT ConsultancyCommented:
Ok, could you post the exact file you are using please?
0
unrealone1Author Commented:
@echo off
SETLOCAL enabledelayedexpansion
REM results from nslookup of cluster8out.eu.messagelabs.com

Set list=192.168.10.1,

REM location for script log file
set log="logfile.txt"
echo Current log file is in %cd%

:Start

for %%a in (%list%) do call :pingit %%a
PING 127.0.0.01 -n 1 -w 5000 > NUL
Goto Start


:pingit

REM This waits upto 2 seconds for a PING response, adjust as wanted and gets the third line of the PING into result
for /f "skip=2 tokens=*" %%r in ('ping -a -n 1 -w 2000 %1') do set result=%%r & goto next
:next

echo %date% %time:~0,5% %1 ... %result%
echo %date% %time:~0,5% %1 ... %result%>>%log%


exit /b

Open in new window

0
Steve KnightIT ConsultancyCommented:
You have a comma after the IP address so it is going to then try and work on the next IP as it sees it... but there isn't one, so if just one then no commas with your script I get

Set list=192.168.10.1,

Open in new window

to
Set list=192.168.10.1

Open in new window


C:\Users\stephen>type logfile.txt
Fri 17/10/2014 15:38 192.168.10.1 ... Request timed out.
Fri 17/10/2014 15:38 192.168.10.1 ... Request timed out.
0
Steve KnightIT ConsultancyCommented:
Though having said that it IS working like that too... this strips out a few not needed lines and gives you some debugging by dumping the whole output into the logfile, it will run for ten pings then open notepad with the log:

@echo on
Set list=182.168.10.1,158.43.128.1

REM location for script log file
set log="logfile.txt"
echo Current log file is in %cd%

:Start
(for /l %%l in (1,1,10) do (
  for %%a in (%list%) do call :pingit %%a %%l
  PING 127.0.0.1 -n 1 -w 1000 > NUL
))>%log% 2>&1

START "" notepad %log%


:pingit

REM This waits upto 2 seconds for a PING response, adjust as wanted and gets the third line of the PING into result
for /f "skip=2 tokens=*" %%r in ('ping -a -n 1 -w 2000 %1') do set result=%%r & goto next
:next

echo %2 %date% %time:~0,5% %1 ... %result%
echo %2 %date% %time:~0,5% %1 ... %result%>con


exit /b
0
unrealone1Author Commented:
Hi I need constant ping every 15 seconds and to output like this:

13/10/2014:11:28 Ping 192.168.xx.xxx Reply from 192.168.xx.xxx time<1ms

I think the first script was on the right track, just need to space the pings out.

Liam
0
Steve KnightIT ConsultancyCommented:
OK so do you have one working for you but want it spacing out?
0
unrealone1Author Commented:
Yes, please.

Tried using a timeout but it doesnt consistantly ping.

The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
22/10/2014 15:28 192.168.10.1 ... Reply fr
=128
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
22/10/2014 15:28 192.168.10.1 ... Reply fr
=128
The system cannot find the file specified.

Open in new window

0
Steve KnightIT ConsultancyCommented:
Can you pst the actual file you are using please as otherwise I suggest changes which might not match what you are doing.

Steve
0
unrealone1Author Commented:
Hi,

Attached

Liam
0
Steve KnightIT ConsultancyCommented:
It's not showing here I'm afraid...
0
Steve KnightIT ConsultancyCommented:
Sorry can see the issue... when there is a <1ms entry in the PING output it is seen as the redirect file input, I only tried on internet based hosts responding in 10ms etc... this wraps the output part in " " then the < is ignored.

Steve

@echo off
 Set list=192.168.10.1
 Set Delay=15

 REM location for script log file
 set log="logfile.txt"

 :Start
   for %%a in (%list%) do call :pingit %%a
   PING 127.0.0.1 -n 1 -w %Delay%000 > NUL

goto :Start

 :pingit

 REM This waits upto 2 seconds for a PING response, adjust as wanted and gets the third line of the PING into result
 for /f "skip=2 tokens=*" %%r in ('ping -a -n 1 -w 2000 %1') do set result="%%r" & goto next
 :next

 echo %date% %time:~0,5% %1 ... %result%>>%log%
 echo %date% %time:~0,5% %1 ... %result%>con

 exit /b 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
unrealone1Author Commented:
Perfect.

Thank you steve for all your work.
0
Steve KnightIT ConsultancyCommented:
No problem, sorry it took a while!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.