[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Batch file. Programming. Comparisons. Greater than less than. if...then... nested for loops.

Posted on 2004-11-30
8
Medium Priority
?
9,841 Views
Last Modified: 2008-10-04
Here's the background to my current situation.  I've got 8 temperamental  adsl lines, 30 computers.  This batch file adds all the routers to the local machine as persistent routes and dns servers:
  route DELETE 0.0.0.0
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.1 METRIC 1 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.2 METRIC 2 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.3 METRIC 3 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.4 METRIC 4 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.5 METRIC 5 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.6 METRIC 6 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.7 METRIC 7 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.8 METRIC 8 -p
  netsh interface ip DELETE dns LOCAL ALL
  netsh interface ip ADD dns LOCAL 192.168.0.1 index=1
  netsh interface ip ADD dns LOCAL 192.168.0.2 index=2
  netsh interface ip ADD dns LOCAL 192.168.0.3 index=3
  netsh interface ip ADD dns LOCAL 192.168.0.4 index=4
  netsh interface ip ADD dns LOCAL 192.168.0.5 index=5
  netsh interface ip ADD dns LOCAL 192.168.0.6 index=6
  netsh interface ip ADD dns LOCAL 192.168.0.7 index=7
  netsh interface ip ADD dns LOCAL 192.168.0.8 index=8
  ipconfig /flushdns

If I run this batch file on every computer, they all start using the same ADSL line.  It does achieve half of it’s objective in that when 192.168.0.1 goes down, all computers start using 192.168.0.2, and then 192.168.0.3.  Now, rather than write 8 different batch files, I want to determine the IP address of the computer and then assign metrics according to which range that ip address is in.  All clients have ips between 192.168.0.11 and 192.168.0.45
Hence the following 8 groups:

  IP           Metrics/Indexes for routers 1 to 8.
  11-15     1,2,3,4,5,6,7,8
  16-20     2,3,4,5,6,7,8,1
  21-25     3,4,5,6,7,8,1,2
  26-30     4,5,6,7,8,1,2,3
  31-35     5,6,7,8,1,2,3,4
  36-40     6,7,8,1,2,3,4,5
  41-45     7,8,1,2,3,4,5,6
  46-50     8,1,2,3,4,5,6,7

The following dos command from within a batch file sets %IPADDRESS% to the last octet. i.e. 11 or 12 or 13:
  for /F "tokens=1-6 delims=:. " %%a in ('ipconfig ^| find "IP Address"') do set IPADDRESS=%%f

After this I get stuck, do I create lists for each of the different metrics?
How do I do a comparison with  %IPADDRESS% to define which list to use?
And then how do I execute
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.1 METRIC 1 –p
  netsh interface ip ADD dns LOCAL 192.168.0.1 index=1
with the metrics from the list based on the %ipaddress%

if you need anything clarified let me know!

ps. please don't suggest linux load-balacing proxy servers that do the same thing, i want to do it all through windows. Also, we're on the edge of a telephone exchanges' catchment area, that's why we've got 8 lines that go down so often and we can't get fibre without a £20,000 install.
0
Comment
Question by:jondanger
  • 3
  • 2
  • 2
7 Comments
 
LVL 9

Accepted Solution

by:
DrWarezz earned 1000 total points
ID: 12705686
Hi,
you're going to want to use the nested IF statements, like so:

for /F "tokens=1-6 delims=:. " %%a in ('ipconfig ^| find "IP Address"') do set IPADDRESS=%%f

:** If the Last Octet is greater than or equal to 46 & it is less than or equal to 50, then ...
IF %IPADDRESS% GEQ 46 ( IF %IPADDRESS% LEQ 50  ECHO [Range: 46-50], do something here )

:** If the Last Octet is greater than or equal to 41 & it is less than or equal to 45, then ...
IF %IPADDRESS% GEQ 41 ( IF %IPADDRESS% LEQ 45  ECHO [Range: 41-45], do something here )

:** Etc ...


Does that help at all?
If the task you want to perform is loads of lines long, then you may want to do something like this:


for /F "tokens=1-6 delims=:. " %%a in ('ipconfig ^| find "IP Address"') do set IPADDRESS=%%f

:** If the Last Octet is greater than or equal to 46 & it is less than or equal to 50, then ...
IF %IPADDRESS% GEQ 46 ( IF %IPADDRESS% LEQ 50  GOTO 46_50 )

:** If the Last Octet is greater than or equal to 41 & it is less than or equal to 45, then ...
IF %IPADDRESS% GEQ 41 ( IF %IPADDRESS% LEQ 45  GOTO 41_45 )

:** Etc ...

GOTO :EOF

:46_50
ECHO Range is 46-50, do something here...
ECHO ... and here ...

GOTO :EOF

:41_45
ECHO Range is 41-45, do something here...
ECHO ... and here ...

GOTO :EOF

:EOF
:** The end here, to any cleanup stuff necessary ...
0
 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 1000 total points
ID: 12707632
Try this:

@echo off

setlocal enabledelayedexpansion

set debug=
REM Remark this line to activate processing
set debug=echo

for /F "tokens=1-6 delims=:. " %%a in ('ipconfig ^| find "IP Address"') do set IPADDRESS=%%f

set /A index=(%IPADDRESS% - 1) / 5

REM <16 (11-15)
if /I %index% LSS 3 set array=1,2,3,4,5,6,7,8
REM 16-20
if /I %index% EQU 3 set array=2,3,4,5,6,7,8,1
REM 21-25
if /I %index% EQU 4 set array=3,4,5,6,7,8,1,2
REM 26-30
if /I %index% EQU 5 set array=4,5,6,7,8,1,2,3
REM 31-35
if /I %index% EQU 6 set array=5,6,7,8,1,2,3,4
REM 36-40
if /I %index% EQU 7 set array=6,7,8,1,2,3,4,5
REM 41-45
if /I %index% EQU 8 set array=7,8,1,2,3,4,5,6
REM >45 (46-50)
if /I %index% GTR 8 set array=8,1,2,3,4,5,6,7

%debug% route DELETE 0.0.0.0

set /A cnt=1

for %%a in (%array%) do (
  %debug% route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.!cnt! METRIC %%a -p
  set /A cnt+=1
)

%debug% netsh interface ip DELETE dns LOCAL ALL

set /A cnt=1

for %%a in (%array%) do (
  %debug% netsh interface ip ADD dns LOCAL 192.168.0.!cnt! index=%%a
  set /A cnt+=1
)

This code has the debug process enabled. To enable it you should remark the 2nd set debug command at the top of the batch file (as noted in remark). Also,  I'm not sure if I got your processing correct in terms of the route and netsh commands so check that out.

Good Luck,
Steve
0
 

Author Comment

by:jondanger
ID: 12713483
Thanks guys, both solutions worked but DrWarez, i've implemented yours. I'll split the points evenly.
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

Author Comment

by:jondanger
ID: 12713489
i did well there didn't i? how do u split points?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 12714935
Go to http://www.experts-exchange.com/Community_Support/ and post a message stating that you didn't split the points as you intended. You might also consider asking them to change the grade to an A. In my eyes, there is no reason for the B.

Thanks :)
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12715514
:-)
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12718888
Thanks. :-)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question