Managing Routes

Using a command-line interface

The following command displays all the static and dynamic routes on a system:

> route print

This command only shows routes that start with 64:

> route print 64.*

To add a temporary route (one that is erased after the system reboots), use this command:

> route ADD <Network> MASK <Mask> <Gateway> MEtrIC <Metric> IF <Interface#>

For example:

> route ADD 157.0.0.0 MASK 255.0.0.0 157.55.80.1 METRIC 3 IF 2

To add a permanent route, use the same command as before except include the -p switch. To delete a route, use this command:

> route DELETE <Network>

For example:

> route DELETE 157.0.0.0

Using VBScript

' This code prints similar information to the "route print" command.

' Since the Win32_IP4RouteTable class was first introduced in

' Windows Server 2003, this script does not work against a

' Windows 2000 server.

' ------ SCRIPT CONFIGURATION ------

strComputer = "."

' ------ END CONFIGURATION ---------

set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

set colRoutes = objWMI.InstancesOf("Win32_IP4RouteTable")

for each objRoute in colRoutes    set colNetworkAdapters = objWMI.ExecQuery(_

                    "select * from Win32_NetworkAdapterConfiguration " & _

                    " where Interfaceindex = " & objRoute.InterfaceIndex )

    for each objNetworkAdapter in colNetworkAdapters

       for each strIP in objNetworkAdapter.IPAddress

          WScript.Echo "Interface: " & strIP

       next

    next

WScript.Echo "Network:   " & objRoute.Destination

    WScript.Echo "NetMask:   " & objRoute.Mask

    WScript.Echo "Gateway:   " & objRoute.NextHop

    WScript.Echo "Metric:    " & objRoute.Metric1

' Other properties you can display:

    ' WScript.Echo "Age: " & objRoute.Age

    ' WScript.Echo "Description: " & objRoute.Description

    ' WScript.Echo "Information: " & objRoute.Information

    ' WScript.Echo "Interface Index: " & objRoute.InterfaceIndex

    ' WScript.Echo "Metric 2: " & objRoute.Metric2

    ' WScript.Echo "Metric 3: " & objRoute.Metric3

    ' WScript.Echo "Metric 4: " & objRoute.Metric4

    ' WScript.Echo "Metric 5: " & objRoute.Metric5

    ' WScript.Echo "Name: " & objRoute.Name

    ' WScript.Echo "Protocol: " & objRoute.Protocol

    ' WScript.Echo "Status: " & objRoute.Status

    ' WScript.Echo "Type: " & objRoute.Type

WScript.Echo

next

' This code shows how to add a route.

' Since the Win32_IP4RouteTable class was first introduced in

' Windows Server 2003, this script does not work against a

' Windows 2000 server.

' ------ SCRIPT CONFIGURATION ------

strComputer = "."

' ------ END CONFIGURATION ---------

set objLocator = CreateObject("WbemScripting.SWbemLocator")

set objWMI = objLocator.ConnectServer(strComputer, "root/CIMv2")

set objR = objWMI.get("Win32_IP4RouteTable").SpawnInstance_( )

objR.Destination = "64.0.0.0"

objR.NextHop = "64.102.57.1"

objR.Mask = "255.0.0.0"

objR.InterfaceIndex = 65539

objR.Metric1 = 22

objR.Protocol = 1

objR.Type = 4

objR.Put_( )
Wscript.Echo "Successfully created route"

If networks are designed properly, system administrators shouldn’t have to worry much about how traffic is being routed. Nevertheless, in certain situations where the network is not fully routed or you are experiencing routing issues, you may need to dig into a server’s routing tables. You can also add static routes to temporarily get traffic flowing the way you want to or force it to go a certain way. However, I do not recommend configuring permanent static routes if you can avoid it. This type of manual configuration if often overlooked or forgotten and can be a headache to track down later unless the configuration changes are well known by all that are maintaining the server.