| | |
DOS Batch command to ping network IP
Please support our Legacy and Other Languages advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
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
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.
|| 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.
Whoops! I messed up the for statement. It should read:
Sorry. =8(
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
•
•
•
•
Whoops! I messed up the for statement. It should read:
Sorry. =8(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
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
I just reread the OP and think that this might do what you need to get done:
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.
@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
Anyway. I would definitely test this prior to using this in a production machine.
•
•
•
•
I just reread the OP and think that this might do what you need to get done:
Becareful with GOTOs as they are really bad way to get a loop done. But, eh.@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
Anyway. I would definitely test this prior to using this in a production machine.
•
•
Join Date: Jun 2007
Posts: 11
Reputation:
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
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
Last edited by hwit; Jun 4th, 2007 at 8:00 pm. Reason: bad gramer needed corections and i wasent clear on one part
•
•
•
•
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
![]() |
Other Threads in the Legacy and Other Languages Forum
- Previous Thread: Euphoria
- Next Thread: Lisp question
| Thread Tools | Search this Thread |






