5

I've a multi-homed windows machine (Windows Server 2016) and I want to make sure that outbound traffic never goes out through secondary network interface (progammatically via C#).

I've 2 default entries for the network interfaces in my routing table:

Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0      172.31.32.1    172.31.44.180     15
          0.0.0.0          0.0.0.0      172.31.96.1    172.31.96.230     15

I think permanently deleting the entry for secondary network interface will be sufficient for my use case. I want only this entry to exist afterwards:

Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0      172.31.32.1    172.31.44.180     15

I found the C# API DeleteIpForwardEntry to delete the route, but I do not know how to make this deletion permanent, so that rebooting the machine doesn't undo my change.

Any help will be appreciated.

user1071840
  • 3,522
  • 9
  • 48
  • 74

5 Answers5

9

In a command prompt

route delete 0.0.0.0 mask 0.0.0.0 172.31.96.1 -p

I'm not sure the -p flag (persistent) works with delete, though. You'll have to test it.

rustyx
  • 80,671
  • 25
  • 200
  • 267
Randy Slavey
  • 544
  • 4
  • 19
  • Thanks, but I need to do this programmatically in C# – user1071840 Jan 24 '18 at 23:32
  • You can use any command prompt process in C#. https://stackoverflow.com/questions/1469764/run-command-prompt-commands – Randy Slavey Jan 24 '18 at 23:48
  • 1
    -p didn't work, the deleted rule was there after reboot – user1071840 Jan 25 '18 at 15:07
  • Bummer, sorry. I think static routes are stored in the registry. You could check there. (HKEY_LOCAL_MACHINE->SYSTEM->CurrentControlSet-> ->Services->Tcpip->Parameters->PersistentRoutes). See https://msdn.microsoft.com/en-us/library/microsoft.win32.registry(v=vs.110).aspx for related c# classes. – Randy Slavey Jan 25 '18 at 20:36
1

This one kind of got me too. The key is to have the -p in the same place as you had in the add command. I too could not get rid of them until I defined all aspects of the route.

route delete -p 0.0.0.0 MASK 0.0.0.0 10.2.2.8 METRIC 410 IF 2

Make sure that you capture every detail accurately. The parameters can be captured from the route table and ipconfig /all command by mapping the IF to the Description on the adapter which gives you the IP and then the Metric can be spotted in the Persistent Routes table and the correct Gateway Address after the Mask.

Essentially, if you still have the command that you did an "add" route with then you can just replace "add" with "delete" and it will remove it. Once I did that I was able to remove the routes.

Good luck.

1

If you want it to persist thru a reboot, consider not specifying a default gateway on the secondary interface (under ipv4 for that interface in control panel) as it's not required.

Alternatively you can adjust the metric to make it a less desirable path and your primary interface will always be favored.

1

The -p argument only applies to the ADD command. Your best bet would be to permanently add a rule that increases the METRIC of the undesired network interface.

route add -p 0.0.0.0 mask 0.0.0.0 172.31.96.1 METRIC 16
WaterPug
  • 11
  • 1
0

route DELETE

The route deletion failed: Element not found.

If you see this error then try NOT putting the Gateway in the command even if you know what it is.

So This Can Work...

   route DELETE 192.168.42.0 MASK 255.255.255.0 METRIC 2 IF 10

But This Would Fail ...

   route DELETE 192.168.42.0 MASK 255.255.255.0 192.168.2.1 METRIC 2 IF 10

So try route DELETE Without the Interface and allow the best guess part of the command figure that part out.
In the above example the value 192.168.2.1 is removed to make it work.

Sql Surfer
  • 1,344
  • 1
  • 10
  • 25