win7 batch

at work every now and then I want to ping computers to see if they are up or not. I work at a hospital, so the security is tight, and we don't get to download software. So I googled how to make a simple batch file to ping a range of IP's. so far I cobbled together this

@ECHO off

SET count=0
SET /p subnet3=What is the 3rd octet?
SET /p subcount=What is the 4th octet?

SET /a subcount=%subcount%+1

ECHO Trying 10.170.%subnet3%.%subcount%

ping -n 1 -w 1000 10.170.%subnet3%.%subcount% >nul  

IF %errorlevel%==0 ECHO 10.170.%subnet3%.%subcount% UP >> c:\install\pingup.log
IF %errorlevel%==1 ECHO 10.170.%subnet3%.%subcount% DOWN >> c:\install\pingdown.log

IF %subcount%==254 goto :eof

GOTO start

this works fine, and the ping log files work. However, at the command prompt the output looks like this.


but I want it to echo if the ip is up or down, so that it would look like this

Trying UP
Trying UP
Trying DOWN
Trying UP

Any Ideas what I am missing in this batch file?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IF %errorlevel%==0 ECHO 10.170.%subnet3%.%subcount% UP& ECHO 10.170.%subnet3%.%subcount% UP >> c:\install\pingup.log
IF %errorlevel%==1 ECHO 10.170.%subnet3%.%subcount% DOWN& ECHO 10.170.%subnet3%.%subcount% DOWN  >> c:\install\pingdown.log

Open in new window

Lee W, MVPTechnology and Business Process AdvisorCommented:
I don't use a batch file.  I use a single command line (which you could save to a batch file:

FOR /L %a in (1,1,254) do @ping -n 1 -w 100 10.170.4.%a | find /i "Reply"

If "%1"=="" Goto Error
REM -n 1 = number of packets = 1; -w 100 = wait 100 ms for a reply - then pipe through find, ignoring case and look for "reply"
FOR /L %a in (1,1,254) do @ping -n 1 -w 100 %1.%a | find /i "Reply"
Goto End
Echo Pings all machines on the subnet specified. 
Echo Usage Example:
Echo   %0 192.168.1
Echo NOTE: Do not include the third period separating the third and forth octets.

Open in new window

JeffBeallAuthor Commented:
wow, you guys are fast!
thank you for the solutions, both work, however, can I ask, NVIT, why does it need so many Echo commands?
I thought that the lines

ECHO Trying 10.170.%subnet3%.%subcount%
ping -n 1 -w 1000 10.170.%subnet3%.%subcount% >nul  
IF %errorlevel%==0 ECHO 10.170.%subnet3%.%subcount% UP

would echo trying 10.170.subnet3.subcount
10.70.subnet3.subcount UP

however, since your solution works and mine doesn't, it obviously needs that extra echo,

& ECHO 10.170.%subnet3%.%subcount% UP

If I look at this, I would have expected something like

trying UP UP
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

With Windows 7 and higher you can't rely on Reply or on the Errorlevel that ping gives back anymore.
If you ping e.g. to a host in the same subnet it's possible that you get an ICMP message with "Reply" in the output but the message actually means that the host is down.
E.g. like:
Reply from Destination host unreachable.

Open in new window

If you get such a message then the errorlevel of ping is also 0.
The IP-address is of the computer where you started the ping from.
Lee W, MVPTechnology and Business Process AdvisorCommented:

In my experience, that's not usually a concern.  Those inappropriate failures generally occur when you don't have an IP or the network isn't connected.  Still, if you think that's a likely scenario, just append another find and use the /v switch which says to find all lines WITHOUT the specified string.  Example:

for /l %a in (1,1,254) do @ping -n 1 -w 100 192.168.1.%a | find /i "reply" | find /v /i "unreachable."

You need 2 ECHOs per UP or DOWN line. One for the visual result; The other to redirect to the file. The & character separates the 2 commands.

Would getting rid of the "ECHO Trying" line suffice? If so, you'll end up with something like: UP

Open in new window


Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Here's a script for mass pinging; it pings several machines at once, so depending on the queue size, you can scan a whole /24 subnet without having enough time to drink a coffee.
You can start it either with a path to a file containing the hosts to ping as first argument, or you can define a set of subnets to scan which you can run with the argument /all, or you can run it without argument, and it will query you for the subnet and first and last host to ping.
It can create a csv file with the results (defined in the ReportFile variable); if ReportFile is not configured, only console output will be shown.
@echo off
setlocal enabledelayedexpansion
set ReportFile=C:\Temp\report.csv
set MainSubnet=10.170
REM Format of subnet definition: <octett1>.<octett2>.<octett3> <First Host> <Last Host>
set Subnet[1]=10.170.1 1 254
set Subnet[1]=10.170.2 1 254
set QueueSize=20

set RegKeyResult=HKCU\Software\Acme\MassPing
set ScriptFullName=%~f0
if /i "%~1"=="/ping" goto StartPing
if not "%~1"=="" goto CommandLineArguments

set Subnet=
set HostFile=%Temp%\%~n0.txt
set /p Subnet=Please enter the subnet to ping: %MainSubnet%.
set /p FirstHost=Please enter the first host IP [1]: 
set /p LastHost=Please enter the last host IP [254]: 
if "%Subnet%"=="" goto :eof
if "%FirstHost%"=="" set /a FirstHost=1
if "%LastHost%"=="" set /a LastHost=254
set HostFile=%Temp%\%~n0.txt
echo Creating subnet file '!HostFile!' ...
(for /l %%i in (%FirstHost%, 1, %LastHost%) do (
	echo %MainSubnet%.%Subnet%.%%i
)) >"!HostFile!"
goto HostFileOK

if "%~1"=="/all" (
	set HostFile=%Temp%\%~n0.txt
	echo Creating subnet file '!HostFile!' ...
	(for /f "tokens=2-4 delims== " %%a in ('set Subnet[') do (
		for /l %%i in (%%b, 1, %%c) do (
			echo %%a.%%i
	)) >"!HostFile!"
) else (
	if not exist "%~1" (
		echo Host name file not found: '%~1'
		exit /b 1
	set HostFile=%~1
for /f %%a in ('find.exe /c /v "" ^<"%HostFile%"') do (set HostsTotal=%%a)
echo [%Time%] Pinging %HostsTotal% hosts ...
set /a HostCount = 0
reg.exe delete "%RegKeyResult%" /f >NUL 2>&1
reg.exe add "%RegKeyResult%" /f >NUL
for /f %%a in ('type "%HostFile%"') do (call :Queue "%%a")
for /f %%a in ('reg.exe query "%RegKeyResult%" ^| find.exe /c /i "Running"') do (set QueueCount=%%a)
if %QueueCount% gtr 0 (
	ping.exe -n 2 localhost >NUL
	goto ResultLoop
echo [%Time%] Done.
if defined ReportFile (>"%ReportFile%" echo "Host","Result")
for /f "tokens=1,3" %%a in ('reg.exe query "%RegKeyResult%" ^| findstr.exe /i "UP DOWN"') do (
	echo %%a	%%b
	if defined ReportFile (>>"%ReportFile%" echo "%%a","%%b")
if defined ReportFile (echo Report written to '%ReportFile%')
reg.exe delete "%RegKeyResult%" /f >NUL 2>&1
goto :eof

:Queue [Host]
set Host=%~1
set /a HostCount += 1
echo [%HostCount%/%HostsTotal%] Queuing %Host% ...
for /f %%a in ('reg.exe query "%RegKeyResult%" ^| find.exe /c /i "Running"') do (set QueueCount=%%a)
if %QueueCount% lss %QueueSize% (
	reg.exe add "%RegKeyResult%" /v "%Host%" /t REG_SZ /d "Running" /f >NUL
	start "MassPing %Host%" /min cmd.exe /c "%ScriptFullName%" /ping %Host%
	goto :eof
ping.exe -n 2 localhost >NUL
goto QueueLoop

set Host=%~2
ping.exe -4 -n 2 %~2 | find.exe /i "TTL" >NUL
if errorlevel 1 (set Result=DOWN) else (set Result=UP)
reg.exe add "%RegKeyResult%" /v "%Host%" /d "%Result%" /f >NUL
goto :eof

Open in new window

JeffBeallAuthor Commented:
thank you so much for all the input. I've learned a lot, which is what I need. I have a lot to go before I can write good batch and script files by myself.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.