954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

DOS Batch command to ping network IP

Hey, I'm a complete noob to batch commands. I just found that they might be somewhat useful for some stuff I'm doing, so I thought I'd try them out.

Anyway, I've got a batch file that calls a program called Poweroff to turn on a pc on my network via WOL (wake on lan).

Here is the code:

CLS
@ECHO ON
poweroff wol -ip 192.168.0.3 -subnet 255.255.255.0 -mac 0xxxx00xxxxx
call wait 90
CLS
EXIT


As you can see, I'm calling a script called wait.bat, that simply pauses the script for 90 seconds to make sure the box has come out of hibernate before I continue processing commands. But I would like it to be more efficient. Rather than just waiting 90 seconds, and then continuing on, I would like to ping 192.168.0.3, and wait till there is a reply, and then go on. That way I can make sure the box is up before continuing.

Anyone know how to check the ping reply to make sure it is getting a reply before continuing on?
Thanks.

edit: I replaced the MAC address for security purposes.

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

*BUMP*
No ideas? Surely it's a simple solution. I've googled it to death but can't find anything. A batch command that could confirm whether a ping comes back successfully would be fairly useful, no? Say:

PING 192.168.0.3 IF Reply { do this } else { do this }

But how do I check for the reply, and pass it to the IF statement?

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

>Surely it's a simple solution

I don't think so, I could only think of a way to get the status back using code i.e getProcessState. Unless someone knows better?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Well thanks for the reply anyway. I think I might have something figured out. As soon as I do, I'll update this thread.
Thanks.

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

If you do a ping -t 192.168....

Then it will continue pinging until you hit CTL C - you will therefore see when it comes up and continue when you are ready.

DenisOxon
Posting Whiz
355 posts since Jan 2007
Reputation Points: 22
Solved Threads: 19
 

If you do a ping -t 192.168....

Then it will continue pinging until you hit CTL C - you will therefore see when it comes up and continue when you are ready.


Yeah, that's great, except you can't do that in a batch script. Thanks for the idea though. I haven't had time to look further into this yet, but as soon as I do, I'll let you know how it goes.

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

i suppose you could do it as a script instead of a batch file

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 
Yeah, that's great, except you can't do that in a batch script. Thanks for the idea though. I haven't had time to look further into this yet, but as soon as I do, I'll let you know how it goes.


Sorry I thought you saw it run, so you could cancel it

DenisOxon
Posting Whiz
355 posts since Jan 2007
Reputation Points: 22
Solved Threads: 19
 

That's ok. Yeah, if and when I do it, it will be going in the middle of the night. But as of right now, I've kind of had to put it on hold.

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

As it will run in middle of night, if it always works with 90 seconds delay then I would go with that.

Just had a thought - when it is up and running can you test for the exstance of a file (have a file used as a marker file that is never deleted) If so you just keep testing for it. Make sure you have an 8.3 name for it.

DenisOxon
Posting Whiz
355 posts since Jan 2007
Reputation Points: 22
Solved Threads: 19
 

I have worked it for you, here are the steps

1) do this once Ping > basefile.txt

This puts results of ping in a file - must be ping that works.

2) then batch file should be

echo off
:start
ping > newfile.txt

fc newfile.txt basefile.txt
if errorlevel 1 goto start
if errorlevel 0 goto nextstep


:nextstep

...... whatever you want to do next when connected

DenisOxon
Posting Whiz
355 posts since Jan 2007
Reputation Points: 22
Solved Threads: 19
 

Just a little trick that I picked up from PERL:

|| first or second -----> Do second if first fails

&& first and second --> Do second only if first succeeds

This is a one liner that I came up with to ping a whole subnet. Very quick and dirty so not as nice as it should be:

for /%g in (1,1,254) do @ping 192.168.20.%g 2>&1 > nul && echo Alive 192.168.20.%g || echo Dead 192.168.20.%g

Remember for statements on the command line only use one %G when dereferencing the variable that is iterated. In a batch file or command script, you must use two percent signs, .i.e., %%G.

mittelgeek
Light Poster
37 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Whoops! I messed up the for statement. It should read:

for /f %g in (1,1,254) do @ping 192.168.20.%g 2>&1 > nul && echo Alive 192.168.20.%g || echo Dead 192.168.20.%g

Sorry. =8(

mittelgeek
Light Poster
37 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Whoops! I messed up the for statement. It should read:

for /f %g in (1,1,254) do @ping 192.168.20.%g 2>&1 > nul && echo Alive 192.168.20.%g || echo Dead 192.168.20.%g
Sorry. =8(

Hi

Would you explain how this works please, I thought I knew DOS commands quite well.

I have tried this from command prompt on and XP machine and I get 'The system cannot find the file 1.'

Ta

Denis

DenisOxon
Posting Whiz
355 posts since Jan 2007
Reputation Points: 22
Solved Threads: 19
 

Its a perl script. It needs to be interpreted

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

Yeah I looked at what I wrote again. FOR /L iterates through a series of numbers using (START,STEP,END) for the parameters.

FOR /F uses the content of (FILENAME or TEXT) as its parameters.

I should have used the /L switch. Try that and it should work.

mittelgeek
Light Poster
37 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

I just reread the OP and think that this might do what you need to get done:

@echo off
cls
poweroff wol -ip 192.168.0.3 -subnet 255.255.255.0 -mac 0xxxx00xxxxx

:return
ping 192.168.0.3 > nul || goto wait

cls
exit

:wait
:: Simulates ~60 second wait
:: Credit goes to www.ss64.com/nt/sleep.html

ping -n 61 127.0.0.1 > nul
:: return
goto return

Becareful with GOTOs as they are really bad way to get a loop done. But, eh.

Anyway. I would definitely test this prior to using this in a production machine.

mittelgeek
Light Poster
37 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

I just reread the OP and think that this might do what you need to get done:

@echo off
cls
poweroff wol -ip 192.168.0.3 -subnet 255.255.255.0 -mac 0xxxx00xxxxx

:return
ping 192.168.0.3 > nul || goto wait

cls
exit

:wait
:: Simulates ~60 second wait
:: Credit goes to www.ss64.com/nt/sleep.html

ping -n 61 127.0.0.1 > nul
:: return
goto return

Becareful with GOTOs as they are really bad way to get a loop done. But, eh.

Anyway. I would definitely test this prior to using this in a production machine.

Thanks, I'll give this a try.

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

ok hi there i think i figered out how to do it
but it's a litle dificult and i need to look up
some codes but heres the trick:

you make a file of a succesfull ping
wich show howmany paackarived at adres sucesfull there should be 4 if
not mistaken

now u save this in a filed colled "good.txt"

then u u make a batch file with a ping that saves it to a txt file called "ping.txt"
and call this .bat file "ping.bat"

then u make a last and finall .bat called "your desired name here.bat" and let the script
call ping.bat
and compare ping.txt with good.txt
if there the same let it display succes
if it is falls let it display failure

this is yust in short but i'll work out the code and explain it in a tuturial here if u guys would like that

hwit
Newbie Poster
11 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
 
ok hi there i think i figered out how to do it but it's a litle dificult and i need to look up some codes but heres the trick: you make a file of a succesfull ping wich show howmany paackarived at adres sucesfull there should be 4 if not mistaken now u save this in a filed colled "good.txt" then u u make a batch file with a ping that saves it to a txt file called "ping.txt" and call this .bat file "ping.bat" then u make a last and finall .bat called "your desired name here.bat" and let the script call ping.bat and compare ping.txt with good.txt if there the same let it display succes if it is falls let it display failure this is yust in short but i'll work out the code and explain it in a tuturial here if u guys would like that


Pardon me - this is exactly what is in my post 22 days ago

DenisOxon
Posting Whiz
355 posts since Jan 2007
Reputation Points: 22
Solved Threads: 19
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You