Windows Batch: ping multiple IP's ouput results in a txt file

Hello experts,

I have the following batch file, reported at:
https://superuser.com/questions/309403/batch-file-to-write-ping-results-to-a-text-file
@echo off

set/p host=host Address: 
set logfile=Log_%host%.log

echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
    timeout 1 >NUL 
    GOTO Ping)

Open in new window


I would like to add some enhancement
-Be able to report the various ip in a config-file.txt
-Be able to output multiple files related to ip reported.
-Parse the time of ping for example I would just to ping during 2 minutes and then exit.


If you have questions, please contact me.

Regards,
LVL 1
LD16Asked:
Who is Participating?
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.

Bill PrewIT / Software Engineering ConsultantCommented:
-Be able to report the various ip in a config-file.txt
By this do you mean read a list of IP addresses to ping from this file?
-Be able to output multiple files related to ip reported.
By this do you mean have one report file for each IP tested, if so how should the file be named?
-Parse the time of ping for example I would just to ping during 2 minutes and then exit.
I don't fully understand this, please expand.


»bp
LD16Author Commented:
1-Yes, read the various IP adress reported in config txt file.
2-Output file should be named with the IP addresses reported in config txt file.
3-I would like to report an specific duration for the ping tracking. I think it can be done with a pause. For example I report 60s this means that the ping reporting will end after 60s of the execution of the script. I don't know if the best is to do this sequencially ping for 60s the first reported IP in config txt file and then continue with the next IP for the same duration or to do this at the same time for the various IP reported in config txt file.

Let me know if you need additional information.
Bill PrewIT / Software Engineering ConsultantCommented:
Give this a try and see how it goes.  Rather than PING for a specific amount of time I did it for a certain number of times.  Much easier to implement, and with some tuning of the iterations setting it might get you close to what you want.

@echo off
setlocal EnableDelayedExpansion

rem Configure settings
set IPlist=B:\EE\EE29123510\iplist.txt
set ReportDir=B:\EE\EE29123510\Report
set LoopCount=10

rem Read each IP to process from input list file
for /f "tokens=*" %%I in ('type "%IPlist%"') do (

    rem Build name of log file from IP
    set LogFile=%ReportDir%\Log_%%I.log

    rem Log header
    echo Target Host = %%I>"!LogFile!"

    rem Log first line from PING as header info, skip other lines
    set SkipRest=
    for /f "tokens=*" %%A in ('ping %%I -n 1 ') do (
        if not defined SkipRest (
            set SkipRest=Y
            echo %%A>>"!LogFile!"
        )
    )

    rem Loop desired count performing PING on this IP
    for /l %%B in (1,1,%LoopCount%) do (
        rem Leave a blank line before each PING result
        echo.>>"!LogFile!"

        rem Do PING, capture output nd log it
        for /f "tokens=* skip=2" %%A in ('ping %%I -n 1 ') do (
            echo %DATE% %TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2% %%A
            echo %DATE% %TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2% %%A>>"!LogFile!"
        )

        rem Delay one second
        timeout 1 >NUL 
    )

)

Open in new window


»bp
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

LD16Author Commented:
Thank you! I will test it as soon as I can.
LD16Author Commented:
Hello Bill,

I tested your proposal however I got the following error message :

The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.

Please find attached my test material.

Thank you again for your help.
29123510_ping-addresses.zip
Bill PrewIT / Software Engineering ConsultantCommented:
PING expects a host name or IP, not a browser URL.  Try the following in yout iplist.txt file:

www.google.com
www.test.com

Open in new window


»bp
LD16Author Commented:
It works, thank you very much for your help.
I was wondering if we can add final enhancements:
-Generate a log with _YYYYMMDDHHMMSS, this will allows me to have multiple logs on a daily basis;
-Create Report folder if it doesn’t exist or if this is to complicated, generate log file in same location of bat file.

Thank you again for your help.
Bill PrewIT / Software Engineering ConsultantCommented:
This should address those:

@echo off
setlocal EnableDelayedExpansion

rem Configure settings
set IPlist=B:\EE\EE29123510\iplist.txt
set ReportDir=B:\EE\EE29123510\Report
set LoopCount=10

if not exist "%IPlist%" (
    echo *ERROR* Missing IPlist file "%IPlist%"
    exit /b
)

if not exist "%ReportDir%" (
    md "%ReportDir%"
)

REM Get current date in YYYYMMDDhhmmss
set TimeStamp=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
    if not defined TimeStamp set TimeStamp=%%A
)
set TimeStamp=%TimeStamp:~0,14%

rem Read each IP to process from input list file
for /f "tokens=*" %%I in ('type "%IPlist%"') do (

    rem Build name of log file from IP
    set LogFile=%ReportDir%\Log_%%I_%TimeStamp%.log

    rem Log header
    echo Target Host = %%I>"!LogFile!"

    rem Log first line from PING as header info, skip other lines
    set SkipRest=
    for /f "tokens=*" %%A in ('ping %%I -n 1 ') do (
        if not defined SkipRest (
            set SkipRest=Y
            echo %%A>>"!LogFile!"
        )
    )

    rem Loop desired count performing PING on this IP
    for /l %%B in (1,1,%LoopCount%) do (
        rem Leave a blank line before each PING result
        echo.>>"!LogFile!"

        rem Do PING, capture output nd log it
        for /f "tokens=* skip=2" %%A in ('ping %%I -n 1 ') do (
            echo %DATE% %TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2% %%A
            echo %DATE% %TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2% %%A>>"!LogFile!"
        )

        rem Delay one second
        timeout 1 >NUL 
    )

)

Open in new window


»bp
LD16Author Commented:
Thank you very much for this proposal Bill.
The objective of this bat is to share with multiple users. I want to avoid that user modify path. Is it possible to set up folder path with:

set mypath=%cd%  or %~dp0?

Thank you very much for your help.
Bill PrewIT / Software Engineering ConsultantCommented:
Yes, that's why I use SET's near the top, to allow you to customize for your needs.  It sounds like you are looking for this, but if something different then just go ahead and adjust there before usage.

@echo off
setlocal EnableDelayedExpansion

rem Configure settings
set IPlist=%~dp0\iplist.txt
set ReportDir=%~dp0\Report
set LoopCount=10

if not exist "%IPlist%" (
    echo *ERROR* Missing IPlist file "%IPlist%"
    exit /b
)

if not exist "%ReportDir%" (
    md "%ReportDir%"
)

REM Get current date in YYYYMMDDhhmmss
set TimeStamp=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
    if not defined TimeStamp set TimeStamp=%%A
)
set TimeStamp=%TimeStamp:~0,14%

rem Read each IP to process from input list file
for /f "tokens=*" %%I in ('type "%IPlist%"') do (

    rem Build name of log file from IP
    set LogFile=%ReportDir%\Log_%%I_%TimeStamp%.log

    rem Log header
    echo Target Host = %%I>"!LogFile!"

    rem Log first line from PING as header info, skip other lines
    set SkipRest=
    for /f "tokens=*" %%A in ('ping %%I -n 1 ') do (
        if not defined SkipRest (
            set SkipRest=Y
            echo %%A>>"!LogFile!"
        )
    )

    rem Loop desired count performing PING on this IP
    for /l %%B in (1,1,%LoopCount%) do (
        rem Leave a blank line before each PING result
        echo.>>"!LogFile!"

        rem Do PING, capture output nd log it
        for /f "tokens=* skip=2" %%A in ('ping %%I -n 1 ') do (
            echo %DATE% %TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2% %%A
            echo %DATE% %TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2% %%A>>"!LogFile!"
        )

        rem Delay one second
        timeout 1 >NUL 
    )

)

Open in new window


»bp

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
LD16Author Commented:
Tested and it works! Thank you again for your help!
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
Windows Batch

From novice to tech pro — start learning today.