Computing.Net > Forums > Programming > cmd - for loop with ping/shutdown

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

cmd - for loop with ping/shutdown

Reply to Message Icon

Name: g1real
Date: February 17, 2009 at 05:50:19 Pacific
OS: Windows XP/Server 2003
Subcategory: Batch
Comment:

I've been messing around for a few days now, trying to get a for loop to work to shutdown certain PC's in certain IP ranges in our network.

I have a textfile containing IP ranges named sd.txt:
192.168.21.1 to 21.254
22.1 to 22.254

Now I have a for loop, that pings every IP adress.

ping -n 1 -w 100 %%i

if ping returns errorlevel 0, IE gets a returned package, shutdown
shutdown -s -m \\%%i -t 600 -c "Long comment here"

The problem is, nothing seems to work, I tried so many variations possible and I bet it's just one or two words here or there that should be changed, the script in it's current state is

@echo off
FOR /F "usebackq" %%I IN (sd.txt) DO (
PING -n 1 -w 100 %%I
IF !ErrorLevel! == 0 shutdown -s -m \\%%I -t 60 -c "Epic fail"
)
pause

Now I've found another topic here about ping not returning %ERRORLEVEL% and I've been messing with setlocal and those variables. Problem is this works, minus the fact shutdown is not executing.



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: February 17, 2009 at 12:55:38 Pacific
Reply:

Your script seems not to have "setlocal enabledelayedexpansion" so !errorlevel! will be treated as a string and not a variable.

Try:

if not errorlevel 1 ......

That way you shouldn't even need delayed expansion or dynamic !variables!.


0

Response Number 2
Name: g1real
Date: February 18, 2009 at 00:08:05 Pacific
Reply:

I knew about the setlocal yeah, but that eventually gave me a can not SETLOCAL thing, together with an infinite loop, but I'll try the if not errorlevel statement and see what it does.


...


Nope it doesn't, it now infinetly loops.


0

Response Number 3
Name: Judago
Date: February 18, 2009 at 02:04:21 Pacific
Reply:

I don't see how it could infinitely loop, it runs over a finite set(text file) and has no goto's or other flow control other than the loop itself.

Do you have privileges to shutdown the remote machines? i.e. Can you shutdown a single machine from the command line?

[edit]
By the way what was the exact setlocal error? Sounds like you machine may be locked down and without command extensions.


0

Response Number 4
Name: Razor2.3
Date: February 18, 2009 at 02:57:33 Pacific
Reply:

Please, please, please tell me your script is named shutdown.bat


0

Response Number 5
Name: Judago
Date: February 18, 2009 at 03:52:17 Pacific
Reply:

I would just like to say:

"Gah, should have thought of that one."

I think you nailed it Razor2.3


0

Related Posts

See More



Response Number 6
Name: g1real
Date: February 18, 2009 at 15:40:05 Pacific
Reply:

It is named shutdown.bat as far as I know, or Shutdown.bat, I don't know if it's cap sensitive.

Why do you ask?


I'll see if I can fish out the setlocal error later, and yes, I do have administrating rights.


0

Response Number 7
Name: Razor2.3
Date: February 18, 2009 at 15:45:22 Pacific
Reply:

It is named shutdown.bat as far as I know, or Shutdown.bat, I don't know if it's cap sensitive.
Change the name. And no, it's not case sensitive.


0

Response Number 8
Name: Mechanix2Go
Date: February 18, 2009 at 16:39:48 Pacific
Reply:

Razor2.3 strikes again.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 9
Name: g1real
Date: February 19, 2009 at 00:18:45 Pacific
Reply:

It seems that the cause isn't lying near me then, as the script was renamed to fit in the schedule that was already tasked for the old program.

Is there any explanation to the naming I can get before I'll test it out happily?


0

Response Number 10
Name: Razor2.3
Date: February 19, 2009 at 00:56:18 Pacific
Reply:

Is there any explanation to the naming I can get before I'll test it out happily?
shutdown.bat: Hey PC, run shutdown for me.
PC: Okay. Now I must look for this, "shutdown." Oh, hey! I found one: shutdown.bat! Let's run it!
Repeat until PC decides it has too many batch files running.

There are methods that affect this scenario beyond renaming, but you seemingly have to come from the DOS era to understand the logic behind them.


0

Response Number 11
Name: Mechanix2Go
Date: February 19, 2009 at 01:47:11 Pacific
Reply:

It's usually a bad idea to name a script the same name as an internal command:
dir
copy
type

or am external command:
find
xcopy
fc

or a reserved word:

con
aux
nul
prn


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 12
Name: g1real
Date: February 19, 2009 at 04:13:52 Pacific
Reply:

I understand now, and yes it seems logical to me.

Shouldn't CMD look in system32, the default folder, for the executable first?

Anyhow, thanks for the help!. It helped a lot, I've been searching for a few days already while the solution was the naming.


0

Response Number 13
Name: Mechanix2Go
Date: February 19, 2009 at 05:33:02 Pacific
Reply:

"Shouldn't CMD look in system32, the default folder, for the executable first?"

nope


=====================================
If at first you don't succeed, you're about average.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: cmd - for loop with ping/shutdown

Exit for loop with arrays in C?! www.computing.net/answers/programming/exit-for-loop-with-arrays-in-c/8687.html

Nested for loops to seperate data www.computing.net/answers/programming/nested-for-loops-to-seperate-data/19438.html

How do I exit from the for loop? www.computing.net/answers/programming/how-do-i-exit-from-the-for-loop/19695.html