Solved

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

Posted on 2014-10-13
28
915 Views
Last Modified: 2014-10-24
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
0
Comment
Question by:unrealone1
  • 14
  • 10
  • 2
  • +2
28 Comments
 
LVL 32

Expert Comment

by:ste5an
ID: 40376687
Describe your desired output??

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

Open in new window

0
 
LVL 8

Expert Comment

by:Wilder_Admin
ID: 40376710
You can also use the free version of http://www.pingplotter.com/ There you have additionally a nice view of your meassured data.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40376715
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
 
LVL 1

Author Comment

by:unrealone1
ID: 40376716
Hi,

Desired output is:

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

Expert Comment

by:Steve Knight
ID: 40376780
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
 
LVL 13

Expert Comment

by:akb
ID: 40376848
As Wilder_Admin said, pingplotter is the way to go
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40377063
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
 
LVL 1

Author Comment

by:unrealone1
ID: 40379324
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40379343
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
 
LVL 32

Expert Comment

by:ste5an
ID: 40379360
btw, when MSDOS means Windows, I would prefer PowerShell and the Test-Connection cmdlet.
0
 
LVL 1

Author Comment

by:unrealone1
ID: 40382389
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40382506
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
 
LVL 1

Author Comment

by:unrealone1
ID: 40384044
Hi Steve,

Still struggling with this. All i get is

"Current log file is in c:\ping testing"
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40384317
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:unrealone1
ID: 40386166
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40386171
Ok, could you post the exact file you are using please?
0
 
LVL 1

Author Comment

by:unrealone1
ID: 40386791
@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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40386809
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40386860
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
 
LVL 1

Author Comment

by:unrealone1
ID: 40394151
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40394267
OK so do you have one working for you but want it spacing out?
0
 
LVL 1

Author Comment

by:unrealone1
ID: 40397021
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40397034
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
 
LVL 1

Author Comment

by:unrealone1
ID: 40399145
Hi,

Attached

Liam
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40399163
It's not showing here I'm afraid...
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 40399205
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
 
LVL 1

Author Closing Comment

by:unrealone1
ID: 40401840
Perfect.

Thank you steve for all your work.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40401863
No problem, sorry it took a while!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Automated script task to send email 22 89
Firewall attack 16 133
Isolating a VM 1 72
Windows Batch File - Count Down 4 49
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now