Solved script to process PING log

May 15, 2016 at 19:12:44
Specs: Windows 7
I really tried to research on the for loop and findstr but no luck.

my log looks like this:

Pinging 10.62.36.161 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 10.62.36.161:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

=================================================

Pinging src.g03.yahoodns.net [98.137.236.150] with 32 bytes of data:
Reply from 98.137.236.150: bytes=32 time=203ms TTL=43
Reply from 98.137.236.150: bytes=32 time=204ms TTL=45
Reply from 98.137.236.150: bytes=32 time=192ms TTL=43
Reply from 98.137.236.150: bytes=32 time=211ms TTL=43

Ping statistics for 98.137.236.150:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 192ms, Maximum = 211ms, Average = 202ms

I want to extract the unsuccessful ip addresses. something like:

Pinging 10.62.36.161 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
ECHO is off.
Ping statistics for 10.62.36.161:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
ECHO is off.
Reply from 8.8.8.8: bytes=32 time=6ms TTL=49
Packets: Sent = 4, Received = 3, Lost = 1 (25% loss),

but I can't seem to remove the 3 lines below.

this is what I have written so far:

@echo off
set log=C:\Users\name\Desktop\log.txt
set test=C:\Users\name\Desktop\test.txt

setlocal EnableDelayedExpansion
rem Assemble the list of line numbers
set numbers=
for /F "delims=:" %%a in ('findstr /i /n /c:"Request timed out" %log%') do (
set /A before=%%a-1, after=%%a+4
set "numbers=!numbers!!before!: !after!: "
)
rem Search for the lines
(for /F "tokens=1* delims=:" %%a in ('findstr /n "^" %log% ^| findstr /B "%numbers%"') do echo %%b) > %test%

pause

am I missing something?
thank you so much if you could help, I have been trying for days.


See More: script to process PING log

Report •


#1
May 16, 2016 at 05:29:43
Honestly, batch doesn't have the best text parsing options available natively, and Window's ping isn't designed to have its text parsed.

What's the backstory here? Are you collecting a series of pings for testing up/down state? If so, you might want to look in to VBScript or PowerShell. Both of them have versions of ping that you can use without attempting text parsing. That'll let your script act immediately on failure/success, or let you write your own, easier to parse, log file.

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#2
May 16, 2016 at 06:50:24
::====== script starts here ===============
::
:: zeb.bat 2016-05-16 20:51:04.90
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=1,2,10 delims= " %%a in (myfile) do (
if %%a equ Pinging set target=%%b
if %%a equ Packets: if %%c equ 4 echo.!target! failed
)
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

#3
May 16, 2016 at 22:13:20
✔ Best Answer
I would put all the ping output, filtered by find, to a text file. Disk space is cheap! That way you can run another batch to filter what you want out of that data.
Assuming a list of ip addresses, f/e: iplist, as input:
@echo off
for /f "tokens=*" %%a in (iplist) do (
echo ping-testing %%a...
for /f "tokens=10" %%b in ('ping %%a ^|find "Lost ="') do >>ping.log echo %%a -%%b- %date:~4% %time%
)
::--------- end snip
this gives you a "database" to work from, which will include ALL the summary ping results for each ip address at any given time/date. Any simple "find" filter from commandline can then capture whichever criteria you want to extract, or it can be sorted on fields as well.
I put the "Lost =" element inside dashes to allow for this:
FIND " -4- " ping.log
which gives all total failures of all IPs. FIND "192.168.0.1" gives all results for that ip. FIND "192.168.0.1 -4-" gives all the full failures for that ip, etc. and you also have date and time available. Since I don't know the "backstory", this is my best effort.

message edited by nbrane


Report •

Related Solutions


Ask Question