alexsnemos
alexsnemos

Reputation: 45

Windows script to add static route for specific IP addresses

I want to create specific routes from command prompt using a script. For that, I need to know a specific address which I can use as a gateway. I want to use this command:

arp -a | findstr /i "a.b.c.d"

Output will be:

Interface: a.b.c.d --- 0x14

Now, I want to modify this IP (by adding +1 at the last octet: so d+1) and make it a default gateway for other IPs (or subnets)

route add x.y.z.w netmask 255.255.255.255 gateway a.b.c.d+1 metric 1

Any idea how can I store retain the output from the initial command, and modify the IP address?

Thanks in advance

Upvotes: 1

Views: 7681

Answers (2)

Ryan Bemrose
Ryan Bemrose

Reputation: 9266

setlocal enabledelayedexpansion

set ipaddr=a.b.c.d

for /f "tokens=1-4 delims=." %%a in ("%ipaddr%") do (
 set /a lastoctet = %%d + 1
 set "newipaddr=%%a.%%b.%%c.!lastoctet!"
)

route add x.y.z.w netmask 255.255.255.255 gateway %newipaddr% metric 1

This code uses a FOR /F to split the address into four parts, then uses SET /A to increment one of the parts, and then reassembles the address. The code assumes you've already extracted the IP address (for use in the findstr command in your example. You can use another for /f to parse out the IP address from the arp output.

for /f "tokens=2 delims= " %%a in ('arp -a ^|findstr /i "Interface"') do set "ipaddr=%%a"

Note that the code above doesn't do any error handling. When the last octet is > 254, the generated IP address will likely be invalid. You should be able to add a check inside the for block to detect this condition.

Upvotes: 1

Hackoo
Hackoo

Reputation: 18837

This code can extract the ip adress and do the same thing as Ryan Bemrose has posted yet.

@echo off
for /f "delims=" %%a in ('arp -a ^|findstr /i "Interface"') do (set Data=%%a)
Call:ExtractIP "%Data%"
Echo The IP adress is : %IP%
pause
Setlocal Enabledelayedexpansion
for /f "tokens=1-4 delims=." %%a in ("%IP%") do (
 set /a lastoctet = %%d + 1
 set "newipaddr=%%a.%%b.%%c.!lastoctet!"
)
Echo route add x.y.z.w netmask 255.255.255.255 gateway %newipaddr% metric 1
EndLocal
pause
Exit /b
::**********************************************************************
:ExtractIP <Data>
(
    echo wscript.echo ExtractIP("%~1"^)
    echo Function ExtractIP(Data^)
    echo Set objRegex = new RegExp
    echo objRegex.Pattern = "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
    echo objRegex.Global = False
    echo objRegex.IgnoreCase = True
    echo Set Matches = objRegex.Execute(Data^)
    echo For Each Match in Matches   
    echo    ExtractIP = Match.Value
    echo Next
    echo End Function
)>"%tmp%\%~n0.vbs"
For /f "delims=" %%# in ('Cscript /nologo "%tmp%\%~n0.vbs"') do set IP=%%#
Exit /b
::**********************************************************************

Edit : or more simple as Ryan Bemrose has posted in the comment to extract the IP adress :

@echo off
for /f "tokens=2 delims= " %%a in ('arp -a ^|findstr /i "Interface"') do (set IP=%%a)
Echo The IP adress is : %IP%
pause
Setlocal Enabledelayedexpansion
for /f "tokens=1-4 delims=." %%a in ("%IP%") do (
 set /a lastoctet = %%d + 1
 set "newipaddr=%%a.%%b.%%c.!lastoctet!"
)
Echo route add x.y.z.w netmask 255.255.255.255 gateway %newipaddr% metric 1
EndLocal
pause
Exit /b

Upvotes: 0

Related Questions