Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

How to add a static route using a PowerShell cmdlet

Add a static route using a PowerShell cmdlet

One of the seldom-known facts about a Windows machine is that it can be configured to perform the functions of a primary router

The Windows operating system has a built-in routing table that enables it to make routing decisions. Suppose you have a small office with a meager IT budget, or you need a primary router for a temporary network that does not require high security. In that case, you can save money by adding routing functions to an existing Windows PC or server rather than purchasing a hardware router. Then, you only need to take care of basic security and ensure that the server you’re using isn’t running short on system resources, such as disk space, memory, or CPU time.

The term routing is used to describe taking a packet from one device and sending it through the network to another device on a different network. First, the logical network address of the destination host is used to get packets through a routed network. Then the host’s hardware address (MAC address) is used to deliver the packet to the correct destination host.

The routing device learns about remote networks from adjoining routing devices or the network administrator. The routing device then builds a routing table (a map of the internetwork) that describes finding the remote networks. If a network is directly connected, the router or routing device already knows how to get to it. If a network isn’t directly related to the router, the router must use one of two ways to learn how to get to the remote network:

  • Dynamic routing: Routing information is dynamically or automatically configured or entered
  • Static routing: Routing information is manually configured or entered

This article will show you how to add static routes and implement static routing on a Windows machine using PowerShell cmdlets.

Why add Static Routes?

Most routers—including the one built into your Windows PC—use some form of dynamic routing. However, there are occasions where static routing may be used; in that case, the network administrator is responsible for manually updating all changes and adding static routes to the routing device. Here are a few occasions where you may need to implement static routes:

  • Due to security considerations, specific routes can’t be added to the default gateway device, and you require another routing device to perform certain functions. In that case, a local Windows computer can be configured to perform the required routing functions.
  • You have set up multiple subnets or VLANs on your network and need to direct traffic to a particular subnet. Static routes can be particularly useful in testing these types of environments.
  • You have two internet routers (primary and secondary) on the network, and you want to use the secondary router for sending social media and audio/video streaming services traffic (Facebook, Instagram, Twitter, YouTube, Netflix, Spotify, etc.), and the primary router for sending all other traffic.
  • You’re currently using a Windows PC as a router for your network, and you want to have better control of the routing table and the flow of traffic.in and out of your network.

What is a Routing Table?

Whenever a network device needs to send data to another device on a network, it must first know where to send it. If the network device cannot directly connect to the destination device, it has to send it via other devices along a route to the destination device. Each device needs to keep track of which way to deliver various packets of data, and for this, it uses what we call a routing table.

A routing table is analogous to a distribution map in package delivery. It is a database that keeps track of paths, like a map, and uses these to determine which way to forward traffic or when the traffic leaves a routing device—whether it’s a physical router or a PC. Therefore, it’s essential to look at your existing routing table before making changes to it. Follow the steps below to view the routing table:

  1. Type ‘powershell’ in the Windows search bar to locate the PowerShell app.
  2. Right-click on the app and click “Run as administrator”.
  3. Once the PowerShell command prompt opens, type the following command to view the routing table: route print.
output of the route print command
Figure 1.0 | Screenshot showing the output of the route print command

As you can see from the screenshot above, the IPv4 routing table consists of the following fields:

  • Network Destination: This field lists all of the network segments that the router is attached to.
  • Netmask: The Netmask column provides the subnet mask not of the network interface attached to the segment but of the segment itself. This allows the router to determine the address class for the destination network.
  • Gateway: Once the router has determined which destination network it needs to send the packet to, it looks at the gateway listing, which tells the router which IP address the packet should be forwarded through to reach the destination network.
  • Interface: The Interface column tells the router which NIC is connected to the appropriate destination network.
  • Metric: The routing metric of the path through which the packet is to be sent. The route will go in the direction of the gateway with the lowest metric.

Persistent Routes: Persistent routes helps to preserve the routes from being removed from the Windows operating system once it is rebooted. This contrasts to non-persistent (active) routes, which are temporary and will be erased after the system is rebooted.

Network Destination Netmask Gateway Interface Metric
0.0.0.0 0.0.0.0 192.168.44.37 192.168.43.57 10
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 1
192.168.0.0 255.255.255.0 192.168.0.100 192.168.0.100 25
192.168.0.100 255.255.255.255 127.0.0.1 127.0.0.1 25

How do you add a Static Route to the Windows Routing Table?

The syntax for adding static route to a routing table in a Windows-based routing device is a follows:

route [/f] [/p] [<command> [<destination>] [mask <netmask>] [<gateway>] [metric <metric>]] [if <interface>]]

The critical parameters for the add static route syntax are described as follows:

  • -f Clears the routing table of all entries that do not host routes (routes with a netmask of 255.255.255.255) and the loopback network route.
  • -p When used with the add command, the specified route is added to the registry and is used to initialize the IP routing table whenever the TCP/IP protocol is started.
  • Command Specifies the command you want to run. The valid commands include: (add, change, delete, print)
  • Destination Specifies the network destination of the route.
  • mask Netmask Specifies the netmask (subnet mask) associated with the network destination
  • Gateway Specifies the forwarding or following hop IP address over which the set of addresses defined by the network destination and subnet mask are reachable
  • Metric Specifies an integer cost metric (ranging from 1 to 9999) for the route, which is used when choosing among multiple routes in the routing table that most closely match the destination address of a packet being forwarded. The route with the lowest metric is chosen.
  • If Interface Specifies the index of the interface over which the destination is reachable.

Now, to show a practical example in PowerShell using the above syntax, follow the steps below to enter a static route to the destination 10.51.0.0 with the subnet mask of 255.255.0.0 and the following hop address of 10.23.0.1:

  1. Type ‘powershell’ in the Windows search bar to locate the PowerShell app.
  2. Right-click on the app and click Run as administrator.
  3. Once the PowerShell command prompt opens, type the following command to add the static route.

route add 10.51.0.0 mask 255.255.0.0 10.23.0.1

Static route with persistent: As stated earlier, this route will only stay in the routing table until Windows is rebooted. Once that happens, the contents of the routing table will be erased. Therefore, if you want the entry to persist, you need to append -p  to the above command. For example, to add a persistent route to the destination 10.51.0.0 with the subnet mask of 255.255.0.0 and the next hop address of 10.23.0.1, type the following command:

route /p add 10.51.0.0 mask 255.255.0.0 10.23.0.1

Static route with metric: To add a route to the destination 10.51.0.0 with the subnet mask of 255.255.0.0, the next hop address of 10.23.0.1, and the cost metric of 65, type the following command:

route add 10.51.0.0 mask 255.255.0.0 10.23.0.1 metric 65

Static route with an interface: The interface list numbers are displayed just before the IPv4 route table when using the route print command to view the routing table (please see figure 2.0 below). Index number 1 (0x1) is for loopback, and the rest are assigned to other existing network interfaces. For example, to add a route to the destination 10.51.0.0 with the subnet mask of 255.255.0.0, the following hop address of 10.23.0.1, and using the interface index 0x7, type the following command:

route add 10.51.0.0 mask 255.255.0.0 10.23.0.1 if 0x7

Screenshot highlighting the interface list numbers
Figure 2.0 | Screenshot highlighting the interface list numbers
Screenshot showing the newly added static route entries
Figure 3.0 | Screenshot showing the newly added static route entries

Conclusion

This article explained how to add a static route and other vital parameters to a routing table using PowerShell. If you want to see the contents of your entries,  use the route print command to view the routing table, and you’ll see all your static route entries. We hope this simple tutorial helps you better understand the concept of the static route and how to implement it in Windows operating systems. Once again, remember to run PowerShell as administrator when executing these commands. If you need more help, type the command route/? to see more syntax examples.

L’article How to add a static route using a PowerShell cmdlet est apparu en premier sur Comparitech.

Enregistrer un commentaire

0 Commentaires