Link to home
Start Free TrialLog in
Avatar of JeffBeall
JeffBeallFlag for United States of America

asked on

win7 batch

hello,
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?

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

Trying 10.170.4.6
Trying 10.170.4.7
Trying 10.170.4.8
Trying 10.170.4.9

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

Trying 10.170.4.6 UP
Trying 10.170.4.7 UP
Trying 10.170.4.8 DOWN
Trying 10.170.4.9 UP

Any Ideas what I am missing in this batch file?
Avatar of NVIT
NVIT
Flag of United States of America image

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

SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JeffBeall

ASKER

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
and
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 10.170.3.2
10.170.3.2 UP 10.170.3.2 UP
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@zalazar,

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."
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.