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

Luis Diaz
Luis Diaz used Ask the Experts™
on
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,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
-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
Luis DiazIT consultant

Author

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 PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Luis DiazIT consultant

Author

Commented:
Thank you! I will test it as soon as I can.
Luis DiazIT consultant

Author

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 PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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
Luis DiazIT consultant

Author

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 PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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
Luis DiazIT consultant

Author

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.
Test your restores, not your backups...
Top Expert 2016
Commented:
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
Luis DiazIT consultant

Author

Commented:
Tested and it works! Thank you again for your help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial