Monday, February 29, 2016

Add Routes in Windows Command Prompt to Control Traffic in PC

If you want to connect to internet using Wi-Fi and connect to the local network using LAN this is the way to do it in Windows.

Open the Command Prompt as administrator

Hit route print

Here is the current routing table of my PC when I connected to both Wi-Fi & LAN networks.





Best path to a destination will be chosen by 2 simple rules

Longest match will be preferred
If there are 2 routes to same destination & mask, lowest metric will be preferred

If you hit ipconfig you can get your gateway IPs. My Wi-Fi gateway is 192.168.37.190 & LAN gateway is 203.115.41.254.

Problem is that there are 2 routes pointed to 0.0.0.0 which is to internet and the lowest metric route is to go through the LAN interface which actually cannot reach internet.

Let's delete all routes to internet (default routes)
Complete command is route delete <destination> mask <mask> <gateway>
To delete all the routes to 0.0.0.0 through any gateway you can hit route delete 0.0.0.0

Let's add a route to internet with a metric of 5 to go through Wi-Fi gateway (192.168.37.190)
Metric of 5 is just a random number which is lower than any route in my routing table. You can give any other number which will suit your routing table.

Complete Command is;
route add <destination> mask <mask> <gateway> metric <metric> if <interface>
Interface is not necessary, It will be added automatically to match the gateway. It is just your IP on the regarding NIC.
Hit route add 0.0.0.0 mask 0.0.0.0 192.168.37.190 metric 5

Now let's add 3 more routes to private ip addresses with a metric of 5 to go through LAN gateway (203.115.41.254)

Hit the following commands
route add 10.0.0.0 mask 255.0.0.0 203.115.41.254 metric 5
route add 172.16.0.0 mask 255.240.0.0 203.115.41.254 metric 5
route add 192.168.0.0 mask 255.255.0.0 203.115.41.254 metric 5




Now you can hit route print again to see the Windows routing table. Task is completed..




































Note: 
To permanently add routes, use -p after route add commands. It will be stored in persistent routes area under the routing table of route print output.

Commands I entered?

route delete 0.0.0.0
route add 0.0.0.0 mask 0.0.0.0 192.168.37.190 metric 5
route add 10.0.0.0 mask 255.0.0.0 203.115.41.254 metric 5
route add 172.16.0.0 mask 255.240.0.0 203.115.41.254 metric 5
route add 192.168.0.0 mask 255.255.0.0 203.115.41.254 metric 5