# Need Help Batch Route table



## mr2 (Oct 14, 2010)

Hi all. I'm new to this forum.
And also I'm new to scripting
I'm trying to make a batch script that will add route based on the IPconfig output. Then it will find IPv4 Address. 
For single NIC I'm able to figure out. But with 2 NICs, I'm having problem. It seems to only look for 1 nic.
Any help is a big help.

Below is my bat file

@echo on
cls
:Configure
:: get VLAN information
FOR /F "tokens=13* delims= " %%A IN ('"ipconfig | find "IPv4 Address""') do set IPADDRESS=%%B
if "%IPADDRESS%"== "10.4.24" goto VLAN24
if "%IPADDRESS%"== "10.1.224" goto VLAN224

:VLAN24
Echo Adding persistent route for VLAN 24
route add -p 10.4.0.0 mask 255.252.0.0 10.4.24.1
goto rprint

:VLAN224
Echo Adding persistent route for VLAN 224
route add -p 10.1.255.0 mask 255.255.255.0 10.1.224.1
goto rprint

::rprint
Echo New persistent route table
route print
pause
:end


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

The For loop only gets the first IP address. Both If statements will fail, as the variable will contain all 4 numbers in the IP address plus a leading space, but you are only checking three.
If will then add the VLAN24 route whether that address was found or not, as you don't have a goto to bypass it.

Try this:

```
@Echo On
Cls
:Configure
:: get VLAN information
For /F "Tokens=2 Delims=:" %%A In ('Ipconfig^| Findstr /I "IPv4"') Do (
  For /F "Tokens=1-3 Delims=. " %%B In ("%%A") Do (
    If "%%B.%%C.%%D"=="10.4.24" (
      Echo Adding persistent route for VLAN 24
      Route Add -P 10.4.0.0 Mask 255.252.0.0 10.4.24.1
    )
    If "%%B.%%C.%%D"=="10.1.224" (
      Echo Adding persistent route for VLAN 224
      Route Add -P 10.1.255.0 Mask 255.255.255.0 10.1.224.1
    )
  )
)
Echo New persistent route table
Route Print
Pause
:end
```


----------

