How to ping multiple servers and return the IP address and hostnames using a script package?

Therefore, you need to use batch for this. Basically, HOSTNAMES servers are listed in a txt file. I used the following code to check all servers and display their results in txtfile.

For /f %%i in (testservers.txt) do ping -n 1 %%i >>pingtest.txt 

All servers pinged the above. Now I want to output the IP addresses and HOST names to a separate file. How can i do this? I know that I can run a for loop to search for words such as "TTL", and then search for the third token (for IP) and words like "PINGING" for the second token (HOSTNAME). But I have errors and cannot display it correctly. The reason I want to display the IP addresses and host names in another file is to create a list of DOWN and UP servers.

Help will be appreciated. :)

EDIT: just so that it doesn't bother, I would like you guys to know that there are 3 different files, testervers.txt has HOSTNAMES, pingtest.txt has ping results, and result.txt will have IP addresses along with hostnames with their current status as DOWN or UP.

+5
source share
7 answers

Well, unfortunately, you did not send your own code either, so that it could be fixed.
Anyway, here is my own solution:

 @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% ) 

The outer loop goes through the hosts, and the inner loop analyzes the ping output. The first two if handle two possible options for resolving an IP address:

  • The host name is the host IP address.
  • The host IP address can be resolved with its name.

If the host IP address cannot be resolved, the address is "ADDRESS N / A".

Hope this helps.

+19
source

Pingtest.txt parsing for each HOST name and the result with the package is difficult because the name and the result are on different lines.

It is much easier to check the result (returned error code) of each PING command directly, rather than redirecting the file. It is also more efficient to wrap the entire construct in parens and redirect the final result only once.

 >result.txt ( for /f %%i in (testservers.txt) do ping -n 1 %%i >nul && echo %%i UP||echo %%i DOWN ) 
+4
source

the problem with ping is that the host is not alive, often your local computer returns a response that the pinged host is unavailable, so the ping error code will be 0, and your code will work with an error because it does not recognize the state.

better do it like that.

 ping -n 4 %1 | findstr TTL if %errorlevel%==0 (goto :eof) else (goto :error) 

so you are looking for a typical ttl string that is always in a well-executed ping result and checks for error on this findstr instead of annoying ping

In general, it looks like this:

 @echo off SetLocal set log=path/to/logfile.txt set check=path/to/checkfile.txt :start echo. some echo date >>%log% :check for /f %%r in (%check%) do (call :ping %%r) goto :eof :ping ping -n 4 %1 | findstr TTL if %errorlevel%==0 (goto :eof) else (goto :error) :error echo. some errormessage to >>%log% echo. some blat to mail? :eof echo. some good message to >>%log% 
0
source

try it

 $servers = Get-Content test.txt $reg="" foreach ($server in $servers) { $reg=$reg+$server+"`t"+([System.Net.Dns]::GetHostAddresses($server) | foreach {echo $_.IPAddressToString})+"`n" } $reg >reg.csv 
0
source

I worked on the code given earlier by Eitan-T and was redesigned for output to a CSV file. The results found in the earlier code do not always give the correct values, so I improved it.

testservers.txt

 SOMESERVER DUDSERVER 

results.csv

 HOSTNAME LONGNAME IPADDRESS STATE SOMESERVER SOMESERVER.DOMAIN.SUF 10.1.1.1 UP DUDSERVER UNRESOLVED UNRESOLVED DOWN 

Pingtest.bat

  @echo off setlocal enabledelayedexpansion set OUTPUT_FILE=result.csv >nul copy nul %OUTPUT_FILE% echo HOSTNAME,LONGNAME,IPADDRESS,STATE >%OUTPUT_FILE% for /f %%i in (testservers.txt) do ( set SERVER_ADDRESS_I=UNRESOLVED set SERVER_ADDRESS_L=UNRESOLVED for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do ( if %%x==Pinging set SERVER_ADDRESS_L=%%y if %%x==Pinging set SERVER_ADDRESS_I=%%z if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN) ) echo %%i [!SERVER_ADDRESS_L::=!] !SERVER_ADDRESS_I::=! is !SERVER_STATE! echo %%i,!SERVER_ADDRESS_L::=!,!SERVER_ADDRESS_I::=!,!SERVER_STATE! >>%OUTPUT_FILE% ) 
0
source

@echo off

set workdir = {your work dir. for example, C: \ work} set iplist =% workdir% \ IP-list.txt

 setlocal enabledelayedexpansion set OUTPUT_FILE=%workdir%\result.csv >nul copy nul %OUTPUT_FILE% echo HOSTNAME,LONGNAME,IPADDRESS,STATE >%OUTPUT_FILE% for /f %%i in (%iplist%) do ( set SERVER_ADDRESS_I=UNRESOLVED set SERVER_ADDRESS_L=UNRESOLVED for /f "tokens=1,2,3" %%x in ('ping -a -n 1 %%i ^&^& echo SERVER_IS_UP') do ( if %%x==Pinging set SERVER_ADDRESS_L=%%y if %%x==Pinging set SERVER_ADDRESS_I=%%z if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN) ) echo %%i [!SERVER_ADDRESS_L::=!] !SERVER_ADDRESS_I::=! is !SERVER_STATE! echo %%i,!SERVER_ADDRESS_L::=!,!SERVER_ADDRESS_I::=!,!SERVER_STATE! >>%OUTPUT_FILE% ) 
0
source

This worked fine, I just add the -a option to ping to solve the hostname problem. Thanks https://stackoverflow.com/users/4447323/wombat

@echo off setlocal enabledelayedexpansion set OUTPUT_FILE = result.csv

 >nul copy nul %OUTPUT_FILE% echo HOSTNAME,LONGNAME,IPADDRESS,STATE >%OUTPUT_FILE% for /f %%i in (testservers.txt) do ( set SERVER_ADDRESS_I=UNRESOLVED set SERVER_ADDRESS_L=UNRESOLVED for /f "tokens=1,2,3" %%x in ('ping -n 1 -a %%i ^&^& echo SERVER_IS_UP') do ( if %%x==Pinging set SERVER_ADDRESS_L=%%y if %%x==Pinging set SERVER_ADDRESS_I=%%z if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN) ) echo %%i [!SERVER_ADDRESS_L::=!] !SERVER_ADDRESS_I::=! is !SERVER_STATE! echo %%i,!SERVER_ADDRESS_L::=!,!SERVER_ADDRESS_I::=!,!SERVER_STATE! >>%OUTPUT_FILE% ) 
0
source

All Articles