Page 1 of 2

BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/ILSE)

Posted: 23 Sep 2014 03:54
by Haris1977
Greetings

I am trying to make a batch file (i guess a *.bat would be ok) but i am not a proggramer. What i want to do is a bat file with 4 steps:


1) Ping a range of ip's (192.168.1.100 to 192.168.1.115)

2) If the script finds >= 2 IP's replies from the specific range (one is 192.168.1.110 which is my server's ip, so it is always "on") it continues to ping the ip's. Thus mean it would loop all ip's all the time. It would be nice a e.g 5 min pause and then restart pinging

3) If the script finds exactly 1 active ip reply (the 192.168.1.110) then my server will go to sleep (not restart, not hybernate).

4) If pc wakes then restart all steps 1-3



I think i can achieve 4 step with the help of a task scheduler.



In other words this is a diagramm with what i want



ping 192.168.1.100 to 192.168.1.115

if active ip reply=1 then run this command –> rundll32.exe powrprof.dll,SetSuspendState 0,1,0

if active ip reply is >=2 then auto restart the script



Anyone?

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 06:07
by foxidrive
This should do what you've specified.

If no active IPs are found it will exit.


Code: Select all

@echo off
:loop
set count=0
for /L %%a in (100,1,115) do (
   echo pinging 192.168.1.%%a
   ping -n 2 192.168.1.%%a |find "TTL=" >nul && set /a count+=1
)
if %count% EQU 1 rundll32.exe powrprof.dll,SetSuspendState 0,1,0
if %count% GTR 1 timeout /t 300 /nobreak & goto :loop
echo exiting
pause

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 06:15
by Haris1977
Thanks foxi but this doesnt work

It just show me this

http://www.2shared.com/photo/1_fNbdHV/Untitled.html

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 06:18
by Haris1977
A friend gave me this (as an expample)

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET BaseRAnge=192.168.1
SET Min=100
SET Max=115
SET Counter=0

FOR /L %%A IN (%Min%,1,%Max%) DO (
ECHO Pinging %BaseRange%.%%A
PING -n 1 %BaseRange%.%%A >nul 2>&1 &&SET /A Counter+=1
)

IF %Counter% GTR 0 (
ECHO Successful ping[s] %Counter%
) ELSE (
ECHO No successful pings
)

but doesnt work, cause it is incomplete!!

I am a programmer noob ....

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 06:39
by foxidrive
Haris1977 wrote:Thanks foxi but this doesnt work

It just show me this

http://www.2shared.com/photo/1_fNbdHV/Untitled.html


Did you call it ping.bat?

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 06:41
by Haris1977
Yes i call it Ping.bat

I run it on my 1rst pc (192.168.1.100) while my other pc (with 192.168.1.110) was in the internet. Maybe thats why? :|

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 06:43
by foxidrive
Don't call it ping.bat

We all learn at some stage that batch files should never use the name of an internal or external command, or program.

pingchk.bat is fine, for example.

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 06:49
by Haris1977
When running it no commands are executed. I will run it again when my second pc (with ip 192.168.1.110) goes offline (i am using it as a nas and now it is active)

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 06:59
by foxidrive
Haris1977 wrote:When running it no commands are executed. I will run it again when my second pc (with ip 192.168.1.110) goes offline (i am using it as a nas and now it is active)


Note that I removed an incorrect ) character at the end of the shutdown command, and added an echo and pause at the end
- copy and paste it again (but not into ping.bat).

Tell me what you see on the console if it doesn't work.

Vista and above have the timeout command.

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 07:15
by Haris1977
Here is a short video with what i take

http://www.2shared.com/video/AnBibuP_/2 ... 60935.html

Shouldnt command lines appear??

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 07:26
by foxidrive
Haris1977 wrote:Shouldnt command lines appear??


You can remove the @echo off and you will see things.

The code is pinging 16 IP address for 2 seconds each, before it executes one of the two commands or exits.

That means you have to run it longer than 32 seconds before ANYTHING will happen. :)

Copy and paste the code again - I added an echo command.

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 07:52
by Haris1977
I test it with my pc. Good thing is that when going to 192.165.1.115 pc does go to sleep if any ip is detected.

I think it works (i ll have to test it though to see it in action)

Problem is that when i wake the pc the script does not auto start again.

It gave me this pic

http://www.2shared.com/photo/age7ecwG/Untitled.html

How can this be auto started again? (remember that i am going to place it in my NUC server and i cannot monitor its actions):)

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 08:28
by foxidrive
Haris1977 wrote:Problem is that when i wake the pc the script does not auto start again.


Put the script in the startup group.

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 08:52
by Haris1977
Doesnt work. Still says press any key to continue

Anyway a friend helped me with this and this is the correct file:

@ECHO OFF & SETLOCAL
:LOOP
SET "_CNT=0"
FOR /L %%A IN (100,1,115) DO (
PING -n 2 192.168.1.%%A|FIND "TTL=">NUL && SET/A _CNT+=1)
IF %_CNT% EQU 1 RUNDLL32 POWRPROF.DLL,SETSUSPENDSTATE 0,1,0
TIMEOUT /T 300 /NOBREAK>NUL
GOTO :LOOP

Thanks foxidrive for your precious help !!

Re: BATCH FILE TO PING MULTIPLE IP'S AND DO AN ACTION (IF/IL

Posted: 23 Sep 2014 08:55
by Squashman
Haris1977 wrote:Doesnt work. Still says press any key to continue

Because Foxidrive put the PAUSE command at the end of the script so you could see the output!