Home Route certain traffic via WiFi in Windows - Powershell
Post
Cancel

Route certain traffic via WiFi in Windows - Powershell

Disclaimer:

I WOULD NOT RECOMMEND USING THIS CODE TO CIRCUMVENT FIREWALLS ETC AT YOUR PLACE OF WORK. THE SAMPLE CODE ON BLOG.MONOTOK.ORG IS PROVIDED “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MONOTOK OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING IN ANY WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Problem:

Sometimes you might need to send certain traffic destined to a FQDN via the WiFi network while the rest of the traffic goes via the Ethernet. An example could be that a certain destination is only reachable via the Ethernet or WiFi but you want the rest of the traffic to go via the other interface.

Windows routing table uses several metrics to decide which interface traffic takes. Normally when both the Ethernet and WiFi are connected, the Ethernet will be preferred over the WiFi; this is decided via the Metric number. This is automatically generated by Windows unless changed, for example the Ethernet will be 10 and the WiFi will be 30. The lower number is preferred. You can also assign a metric to a static route inserted into the routing table however the routes metric is added to the interface metric. This prevents the route overriding the Ethernet even if you make the interfaces the same metric and then remove the WiFi default route (Windows kept inserting it again anyway).

The solution is to manually change the metrics of the interfaces. Change the Ethernet and WiFi metric to 5 and remove the default static routes. Then enter new static routes, WiFi route metric of 5 and Ethernet metric of 3. Then insert the new static route to the specific destination with a metric of 1.

This will give you the follow route metrics:

  • Ethernet 8
  • WiFi 10
  • Specific WiFi Route 6

Obviously this is not quick and simple to configure so I have written a Powershell script. Just open it in the Windows PSE or run it from the Powershell. There are two scripts, the first changes the routing table while the second will revert all changes. Make sure to leave the WiFi and Ethernet connected before running the undo script.

Configure Routes Script

$exampleORG = [System.Net.Dns]::GetHostAddresses(“example.org“)

$exampleIP = $exampleORG.IPAddressToString

Write-Output "Setting the WiFi interface to Metric 5"

Set-NetIPInterface -InterfaceAlias WiFi -AddressFamily IPv4 -InterfaceMetric 5

Write-Output "Setting the Ethernet interface to Metric 5"

Set-NetIPInterface -InterfaceAlias Ethernet -AddressFamily IPv4 -InterfaceMetric 5

#Get the wifi adapter nexthop

$wifiNextHop = Get-NetIPConfiguration -InterfaceAlias WiFi | Select -ExpandProperty IPv4DefaultGateway | foreach {$_.NextHop}

#Get the ethernet adapter nexthop

$ethNextHop = Get-NetIPConfiguration -InterfaceAlias Ethernet | Select -ExpandProperty IPv4DefaultGateway | foreach {$_.NextHop}

Write-Output "Setting the routes"

#Remove all default routes

Remove-NetRoute -InterfaceAlias WiFi -NextHop $wifiNextHop

Remove-NetRoute -InterfaceAlias Ethernet -NextHop $ethNextHop

#Add the correct static routes

New-NetRoute -DestinationPrefix ($exampleIP+"/32") -InterfaceAlias WiFi -AddressFamily IPv4 -NextHop $wifiNextHop -RouteMetric 1 -PolicyStore ActiveStore

New-NetRoute -DestinationPrefix ("0.0.0.0"+"/0") -InterfaceAlias WiFi -AddressFamily IPv4 -NextHop $wifiNextHop -RouteMetric 5 -PolicyStore ActiveStore

New-NetRoute -DestinationPrefix ("0.0.0.0"+"/0") -InterfaceAlias Ethernet -AddressFamily IPv4 -NextHop $ethNextHop -RouteMetric 3 -PolicyStore ActiveStore

Write-Output "Done"

Undo Code

$exampleORG = [System.Net.Dns]::GetHostAddresses(“example.org“)

$exampleIP = $exampleORG.IPAddressToString

Write-Output "Setting the WiFi interface to Metric auto"

Set-NetIPInterface -InterfaceAlias WiFi -AddressFamily IPv4 -AutomaticMetric Enabled

Write-Output "Setting the Ethernet interface to Metric auto"

Set-NetIPInterface -InterfaceAlias Ethernet -AddressFamily IPv4 -AutomaticMetric Enabled

#Remove specific route

Remove-NetRoute -InterfaceAlias WiFi -DestinationPrefix ($exampleIP+"/32")

#Add the WiFi default route

#New-NetRoute -InterfaceAlias WiFi -DestinationPrefix "0.0.0.0/0"

 

Hopefully you will find this code useful.

This post is licensed under CC BY 4.0 by the author.

If you have found this site useful, please consider buying me a coffee :)

Proud supporter of the Gnome Foundation

Become a Friend of GNOME

Contents

EVE-NG access over Internet - Reverse Proxy

Linux - Move back in time backups

Comments powered by Disqus.