Batch file to MASS ping group of computers on network by name, check reply, and resolve hostname

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.


Do you really need to ping? The computer account will automatically change its password every 30 days. On a 200/2003/2008 functional level you can use dsquery computer -stalepwd X where X is the number of days since the last password change. This process is usually automated by moving the computer accounts that respond to this to an "OLD" OU and then after an additional 30-90 days they are automatically deleted if the password still has not changed.


You can also use nmap for scanning:

nmap -sn -PE -oG scan.txt 192.168.1.1 192.168.2.0/24

Quoting from the man page:

-sn: Ping Scan - disable port scan
   The default host discovery done with -sn consists of an ICMP echo request,
   TCP SYN to port 443, TCP ACK to port 80, and an ICMP timestamp request
   by default.

-oN/-oX/-oS/-oG <file>: Output scan in normal, XML, s|<rIpt kIddi3,
   and Grepable format, respectively, to the given filename.

-PE; -PP; -PM (ICMP Ping Types) .
   In addition to the unusual TCP, UDP and SCTP host discovery types discussed
   previously, Nmap can send the standard packets sent by the ubiquitous ping
   program. Nmap sends an ICMP type 8 (echo request) packet to the target IP
   addresses, expecting a type 0 (echo reply) in return from available hosts.
   [...] Use the -PE option to enable this echo request behavior.

scan.txt will look something like this:

# Nmap 5.50 scan initiated Fri Aug 19 17:59:59 2011 as: nmap -vv -sn -PE -oG /tmp/scan.txt  192.168.1.1 192.168.2.0/24
# Ports scanned: TCP(0;) UDP(0;) SCTP(0;) PROTOCOLS(0;)

Host: 192.168.1.1 ()    Status: Down
Host: 192.168.2.0 (www.dummy.example.org)   Status: Up
Host: 192.168.2.1 (www.foo.example.org) Status: Down
...
# Nmap done at Fri Aug 19 18:03:26 2011 -- NNN IP addresses (1 host up) scanned in 4.02 seconds