Batch File to Mass Ping Computers on Network – Script Example

active-directoryscriptingwindows

So, I have a list of 5000 computers on my Windows Active Directory network that may or may not still exist (I know, don't ask…I need to know for a project, and of course little help from the network people, and so many errors in the data they give me)

The computer names fall within SAT1 to SAT5000.
However, some of them may have been upgraded to a new OS and renamed. so in this case i would like to detect the new name as well.

I wonder if someone might have a script that would, given a text file containing a list of computer names, for each:
1. ping the computer to check for existence (yes, it must be on, I know)
2. after receiving the ip from the 1st ping, do a ping -a to get a hostname
3. write the results out to a text file

(Even better…is it possible to somehow split the initial file into mutliples, and spawn several batch files to run concurrently, to get down the slowness of synchronously pinging 5000 machines??)

Update

This article seems to be somewhat related to what I am looking for:
http://www.enterpriseitplanet.com/networking/features/article.php/1571771

Update 2

This is what I ended up with:

@echo off
rem del output.txt
rem Loop thru list of computer names in file specified on command-line

for /f %%i in (%1) do call :check_machine %%i
goto end

:check_machine

rem Check to see if machine is up.
echo %1
ping -n 2 %1 >NUL 2>NUL
if errorlevel 1 goto down

rem Reverse-lookup machine name and report
  for /f "usebackq tokens=2,3" %%d in (`ping -n 1 -a %1 ^| find "Pinging "`) do echo %1, %%d,%%e >> output.txt
goto end

:down
  rem Report machine down
  echo %1 >> output.txt

:end

And output is in this format:

SAT10 
SAT1209 
SAT601, CGY2601.na.sat.com,[110.3.111.70] 
SAT3592, CGY3592.na.sat.com,[110.0.237.45] 

If you split the computers list into mutliple smaller files, you can ping asynchronously like so:

del output.txt
start MassPing.cmd Computers1.txt
start MassPing.cmd Computers2.txt
start MassPing.cmd Computers3.txt
start MassPing.cmd Computers4.txt
start MassPing.cmd Computers5.txt
start MassPing.cmd Computers6.txt

Best Answer

Here's a batch file for you:

@echo off
rem Loop thru list of computer names specified on command-line
for /f %%i in (%1) do call :check_machine %%i
goto end

:check_machine

rem Check to see if machine is up.
ping -n 2 %1 >NUL 2>NUL
if errorlevel 1 goto down

rem Reverse-lookup machine name and report
for /f "usebackq tokens=2" %%d in (`ping -n 1 -a %1 ^| find "Pinging"`) do echo %1:Up:%%d
goto end

:down
rem Report machine down
echo %1:Down

:end

Pass it a text file with a list of computer names in it and it'll PING them (2 tries-- you can increase that by increasing the number after "-n" on the 1st PING command-line), and if it gets a response, perform a reverse-lookup of the name. It returns results as:

computer-name-1:Up:name-it-resolved-to
computer-name-2:Down
computer-name-3:Up:name-it-resolved-to
...

To run multiple in parallel, just make multiple text files w/ different lists of machine names and launch a few copies in parallel.

Quick and dirty saves the day.

Related Topic