Windows XP Cookbook (Cookbooks)

Problem

You want to view the routing table on a system or possibly configure static routes. Configuring static routes generally isn't needed with today's networks, but it can be necessary especially when working in restricted lab environments that are not fully routed.

Solution

Using a command-line interface

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

> route print

For a good overview of what each column represents in the route print output, see MS KB 140859.

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#>

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>

Example:

> route DELETE 157.0.0.0

Using VBScript

' This code prints similar information to the "route print" command. ' ------ SCRIPT CONFIGURATION ------ strComputer = "." ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set colRoutes = objWMI.InstancesOf("Win32_IP4RouteTable") for each objRoute in colRoutes 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. ' ------ 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"

Discussion

If networks are designed properly, you 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 system's routing tables a bit. 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, we do not recommend configuring permanent static routes if you can avoid it. This type of manual configuration if often overlooked or forgotten about and can be a headache to track down later unless the configuration changes are well known by all who are maintaining the system.

See Also

MS KB 140859, "TCP/IP Routing Basics for Windows NT," and MS KB 157025, "Default Gateway Configuration for Multihomed Computers"

Категории