Batch File: Run simultaniously

Hi there,

I need to have all subnets to be scanned simultaniously.

Thanks,
Rene

@ECHO OFF

SETLOCAL EnableDelayedExpansion

SET Subnets=192.168.20,192.168.21,192.168.22,192.168.23,192.168.24,192.168.25
SET LogFile=%~dpn0.log
SET Alarm=0

REM READING LOG FILE
IF EXIST "%LogFile%" FOR /F "usebackq tokens=3,4 delims=," %%A IN ("%LogFile%") DO SET Var.%%A=%%B


:Home
FOR %%S IN (%Subnets%) DO (
   FOR /L %%A IN (1,1,254) DO (
      SET IP=%%S.%%A
      PING -n 1 -w 500 !IP! >NUL && (
         TITLE IP MONITOR [!IP!] OK
         IF "!Var.%%S.%%A!" NEQ "OK" (
            ECHO %date%,%time% [!IP!] CHANGED TO [OK]
            ECHO %date%,%time%,!IP!,OK>>"%LogFile%"
            SET Alarm=1
         )
         SET Var.%%S.%%A=OK
      ) || (
         TITLE IP MONITOR [!IP!] DISCONNECTED
         IF "!Var.%%S.%%A!" NEQ "DISCONNECTED" (
            ECHO %date%,%time% [!IP!] CHANGED TO [DISCONNECTED]
            ECHO %date%,%time%,!IP!,DISCONNECTED>>"%LogFile%"
            SET Alarm=1
         )
         SET Var.%%S.%%A=DISCONNECTED
      )
   )
   IF !Alarm! == 1 ECHO      
   SET Alarm=0
)

SET Var. >"%~dpn0_Array.txt"

PING -n 1 -w 5 10.0.0.0 >NUL
GOTO Home

Open in new window

LVL 10
ReneGeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AngryBinaryCommented:
If you want to scan all subnets asynchronously (without having to wait for one scan to complete before starting another), then you should create a batch script that takes a single sub-net as a parameter and scans it:

# scansubnet.bat
   FOR /L %%A IN (1,1,254) DO (
      SET IP=%1.%%A
      PING -n 1 -w 500 !IP! >NUL && (
         TITLE IP MONITOR [!IP!] OK
         IF "!Var.%1.%%A!" NEQ "OK" (
            ECHO %date%,%time% [!IP!] CHANGED TO [OK]
            ECHO %date%,%time%,!IP!,OK>>"%LogFile%"
            SET Alarm=1
         )
         SET Var.%1.%%A=OK
      ) || (
         TITLE IP MONITOR [!IP!] DISCONNECTED
         IF "!Var.%1.%%A!" NEQ "DISCONNECTED" (
            ECHO %date%,%time% [!IP!] CHANGED TO [DISCONNECTED]
            ECHO %date%,%time%,!IP!,DISCONNECTED>>"%LogFile%"
            SET Alarm=1
         )
         SET Var.%1.%%A=DISCONNECTED
      )
   )
   IF !Alarm! == 1 ECHO      
   SET Alarm=0

Open in new window


Then, your main script would look like this (using the "START" invocation to start each individual scan without waiting for it to complete before moving on to the next):

 
#scansubnets.bat

@ECHO OFF

SETLOCAL EnableDelayedExpansion

SET Subnets=192.168.20,192.168.21,192.168.22,192.168.23,192.168.24,192.168.25
SET LogFile=%~dpn0.log
SET Alarm=0

REM READING LOG FILE
IF EXIST "%LogFile%" FOR /F "usebackq tokens=3,4 delims=," %%A IN ("%LogFile%") DO SET Var.%%A=%%B


:Home
FOR %%S IN (%Subnets%) DO (
    START scansubnet.bat %%S
)

SET Var. >"%~dpn0_Array.txt"

PING -n 1 -w 5 10.0.0.0 >NUL
GOTO Home

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AngryBinaryCommented:
Sorry, that last post was a little sloppy, since I didn't consider the local variables LogFile and Alarm - but hopefully you get the gist.
0
ReneGeAuthor Commented:
Good idea!

I wondered if it can use START to call a sub-routine.

This was, I could run multiple sub-routines simultaniously!!

But, as expected, it did'nt work :(

@echo off

start "" :test 1
start "" :test 2
pause
exit

:Test
echo No%1
ping -n 1 -w 2000 10.0.0.0 >NUL
EXIT /b

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ReneGeAuthor Commented:
Thanks for your help!
0
Steve KnightIT ConsultancyCommented:
Rene,

You can't get it to start a subroutine BUT you can soon start a sub using a parameter, i.e. something like this... i.e. it STARTS itself, passes param of sub to run, then any params for that.  If there is any command line optoio

@echo off
if /i "%~1" =="goto" %*

REM normal run comes here
START "" "%~f0" goto :test 1
exit /b

REM Second run comes here:
:test
 echo Run no. %1
exit /b
0
Steve KnightIT ConsultancyCommented:
Used similar in that hangman Q that didn't quite make it anywhere yet:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_27387453.html#36947012

Steve
0
ReneGeAuthor Commented:
Thanks Steve,
Sounds very cool and a good contribution to another recent post from Paul. I'll look at it beginning of next week.

Now tuning off !!

Cheers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.