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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Haris1977
Posts: 16
Joined: 23 Sep 2014 03:48

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

#1 Post by Haris1977 » 23 Sep 2014 03:54

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?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#2 Post by foxidrive » 23 Sep 2014 06:07

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

Haris1977
Posts: 16
Joined: 23 Sep 2014 03:48

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

#3 Post by Haris1977 » 23 Sep 2014 06:15

Thanks foxi but this doesnt work

It just show me this

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

Haris1977
Posts: 16
Joined: 23 Sep 2014 03:48

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

#4 Post by Haris1977 » 23 Sep 2014 06:18

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#5 Post by foxidrive » 23 Sep 2014 06:39

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?

Haris1977
Posts: 16
Joined: 23 Sep 2014 03:48

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

#6 Post by Haris1977 » 23 Sep 2014 06:41

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? :|

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#7 Post by foxidrive » 23 Sep 2014 06:43

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.

Haris1977
Posts: 16
Joined: 23 Sep 2014 03:48

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

#8 Post by Haris1977 » 23 Sep 2014 06:49

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)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#9 Post by foxidrive » 23 Sep 2014 06:59

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.

Haris1977
Posts: 16
Joined: 23 Sep 2014 03:48

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

#10 Post by Haris1977 » 23 Sep 2014 07:15

Here is a short video with what i take

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

Shouldnt command lines appear??

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#11 Post by foxidrive » 23 Sep 2014 07:26

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.

Haris1977
Posts: 16
Joined: 23 Sep 2014 03:48

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

#12 Post by Haris1977 » 23 Sep 2014 07:52

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):)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#13 Post by foxidrive » 23 Sep 2014 08:28

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


Put the script in the startup group.

Haris1977
Posts: 16
Joined: 23 Sep 2014 03:48

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

#14 Post by Haris1977 » 23 Sep 2014 08:52

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 !!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#15 Post by Squashman » 23 Sep 2014 08:55

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!

Post Reply