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.
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