title: Bat script for automatically adding routes - Must be run several times
body: <p>I'm writing a bat script for automatically creating /32 routes towards devices in a network reachable trough a LAB tunnel interface. </p>
  
  <p>I have to do this because the LAB tunnel has to be set upon an other one (corporate tunnel) that automatically forwards packets inside of it if there is no routes for the destination. Consequently I create all /32 routes for devices in the network in order to prevent forwarding the corporate tunnel.</p>
  
  <p>Following script makes the trick but for an unknown reason to me, I have to run it 3 or 4 times before it works. (Note I'm a big noob with bat script)</p>
  
  <pre><code>@echo off
  c:
  cd %systemroot%
  set /P input=Please enter a LAB ID:
  set /A labid=%input%
  if %labid% GTR 98 ( 
      if %labid% LSS 255 (
          set "net=10.%labid%"
          for /f "tokens=1-5 delims= " %%A in ('route print ^| findstr %net%') do (
              echo Adding static routes for LAB %labid%...
              set gatewayssl=%%C
              echo Gateway is SSL interface: %gatewayssl%
              for /l %%h in (1,1,254) do call :add_route %%h %gatewayssl%
              goto:EOF
          )
          goto:EOF
      )else (
          echo Invalid Lab ID
          goto:EOF
      )
  ) else (
      echo Invalid Lab ID
          goto:EOF
  )
  
  :add_route
  set ipaddr=%net%.0.%1
  route add %ipaddr% mask 255.255.255.255 %2% metric 1
  goto:EOF
  </code></pre>
  
  <p>Typically, here is the ouput produced:</p>
  
  <pre><code>[...]&gt;Z:\ALU\SGCC\LAB\labrouting.bat
  Please enter a LAB ID:104
  FINDSTR : Ligne de commande erronée
  
  C:\WINNT&gt;Z:\ALU\SGCC\LAB\labrouting.bat
  Please enter a LAB ID:104
  Adding static routes for LAB 104...
  Gateway is SSL interface:
  
  Manipule les tables de routage du réseau.
  
  ROUTE [-f] [-p] [cmde [destin]
  [route manual apperas many times because of the for loop...]
  
  C:\WINNT&gt;Z:\ALU\SGCC\LAB\labrouting.bat
  Please enter a LAB ID:104
  Adding static routes for LAB 104...
  Gateway is SSL interface: 192.168.104.1
  </code></pre>
  
  <p>As you can, after running this script 3 times, it finally works. 
  Could you please help to identify what causes this issue ?</p>
  
  <p>I thank you in advance for your very appreciated support.</p>
  
  <p>Best regards,</p>
  
  <p>Sylvain.</p>