Ping a list of IPs with strings of names listed in a txt file and must use batch

2

1

I want to ping a list of IPs with a string of text entered next to it. The text will be multiple words and have numbers. The IPs all start with 10.x.x.x.

This is a script that I was looking into, but it tries to resolve the IP of the IP that I put into it. I guess it would work if I put the hostnames in there. Maybe I should keep that in there just in case.

@echo off
setlocal enabledelayedexpansion

set OUTPUT_FILE=result.txt
>nul copy nul %OUTPUT_FILE%
for /f %%i in (testservers.txt) do (
    set SERVER_ADDRESS=ADDRESS N/A
    for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
        if %%x==Pinging set SERVER_ADDRESS=%%y
        if %%x==Reply set SERVER_ADDRESS=%%z
        if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
    )
    echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! >>%OUTPUT_FILE%
)

What I really want is to have a text string like "This is the Server XYZ" to be concatenated at the end of line for the result.txt file.

So my testservers.txt file will look like this:

10.0.0.1 This is the Server ABC.
10.0.0.2 This is the Server DEF.
hostname1 This is the Server LMN.
hostname2 This is the Server XYZ.

When I run it now, it spits out results like this into the result.txt file.

10.0.0.1 [10.0.0.1] is UP
10.0.0.1 [10.0.0.2] is UP
hostname1 [10.0.0.3] is UP
hostname2 [10.0.0.4] is UP

Then the result.txt file would look like this:

10.0.0.1 [10.0.0.1] is UP This is the Server ABC.
10.0.0.2 [10.0.0.2] This is the Server DEF.
hostname1 [10.0.0.3] This is the Server LMN.
hostname2 [10.0.0.4] This is the Server XYZ.

Hope I provided enough information. Let me know if I didn't.

rockower

Posted 2017-12-28T22:54:30.007

Reputation: 134

Answers

2

Since you are using a FOR /F loop to read thru the text document content of testservers.txt then you can simply add "TOKENS=1,*" and the first token will be the the server name or IP address per each line and the next token will be the remaining portion of each line after that.

This means you can then utilize the next token of the FOR /F loop to get the remaining portion of each line after the first token and append that to the ECHO line for the %OUTPUT_FILE%.

testservers.txt

enter image description here


Example Script

@echo off
setlocal enabledelayedexpansion

set OUTPUT_FILE=result.txt
>nul copy nul %OUTPUT_FILE%
for /f "tokens=1,*" %%i in (testservers.txt) do (
    set SERVER_ADDRESS=ADDRESS N/A
    for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
        if %%x==Pinging set SERVER_ADDRESS=%%y
        if %%x==Reply set SERVER_ADDRESS=%%z
        if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
    )
    echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! %%j >>%OUTPUT_FILE%
)

Output Results

10.0.0.1 [10.0.0.1] is DOWN This is the Server ABC. 
10.0.0.2 [10.0.0.2] is DOWN This is the Server DEF. 
hostname1 [<IP Address>] is UP This is the Server LMN. 
hostname2 [<IP Address>] is UP This is the Server XYZ. 

Further Resources

  • FOR /F

  • FOR /?

        tokens=x,y,m-n  - specifies which tokens from each line are to
                          be passed to the for body for each iteration.
                          This will cause additional variable names to
                          be allocated.  The m-n form is a range,
                          specifying the mth through the nth tokens.  If
                          the last character in the tokens= string is an
                          asterisk, then an additional variable is
                          allocated and receives the remaining text on
                          the line after the last token parsed.
    

Pimp Juice IT

Posted 2017-12-28T22:54:30.007

Reputation: 29 425

1Your accurate solution works with or without the message after the IP or Hostname.. Additionally, thank you for the reference to the FOR /F resource. – rockower – 2018-01-03T18:37:17.313