4

This is the way how many of us do set a route on windows:

route add 10.10.0.0 mask 255.255.0.0 10.77.77.1 if 58 -p

In the command line 58 is the Interface Index (IF) and most of the time its value is constant for a specific network interface card. Therefore one can create a CMD batch file to automate deletion and re-setting of all the routes she/he need.

However, in my case I have a USB 3.0 to Dual Port Gigabit Ethernet Adapter NIC, and its IFs change every time when I connect to the USB port of my notebook. What I do, just open a cmd shell, get the current Interface IDs by giving route print, then open the CMD batch file with text editor and change all the old IF (ex: 58) values, save&close, and at last run the CMD batch file on the shell.

If there is a way to set a route only by knowing the MAC address, not depending of the IF - then the problem is solved :) The solution may be a simple one line CMD command (set route by MAC), or maybe first get the IF by MAC and set route by IF (set route by IF of the MAC).

Any help is highly appreciated!

2

Next code snippet shows how-to get the IF by MAC using FOR /F loop against result of WMIC (Windows Management Instrumentation Command):

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion

set "_maca=AA:AA:AA:AA:AA:AA"            change to match your circumstances
set "_geta=InterfaceIndex^, MACAddress"    to keep next lines in reasonable length
set "_ifin="                               to ensure that `_ifin` variable is empty

for /F "tokens=1,*" %%G in ('
    WMIC NIC where "MACAddress='%_maca%'" get %_geta% 2^>NUL ^| find ":"
                            ') do set "_ifin=%%~G"

if defined _ifin (
        rem route command block here:
    route add 10.10.0.0 mask 255.255.0.0 10.77.77.1 if %_ifin% -p
) else (
    echo %_maca%: No Instance^(s^) Available.
)

Resources (required reading):

  • Thank you very much @JosefZ, this is the solution to my problem! – uzeyirs Sep 17 '16 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.