We help IT Professionals succeed at work.

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

Comment
Watch Question

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

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.

Author

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

Author

Commented:
Thanks for your help!

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

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

Author

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