Learn how to connect to clients with different IP ranges on OpenVPN using PowerShell commands to change the metrics of network adapters.
OpenVPN is a popular VPN software that allows you to create secure and encrypted connections between your devices and remote networks. However, sometimes you may encounter a problem when you need to connect to clients that have the same IP range as your local network. This can cause conflicts and prevent you from accessing the resources you need. In this article, we will show you how to solve this problem by changing the metrics of your network adapters using PowerShell commands.
Problem
The problem occurs when you have two network adapters that support the same IP range. For example, suppose your local network has the subnet 192.168.1.0/24, and you use OpenVPN to connect to a remote network that also has the same subnet. When the VPN is active, you have two network adapters on your computer: one for the local network and one for the VPN. Windows will use the network adapter with the lower metric to resolve IP addresses. Usually, the VPN driver ensures that its network adapter has a lower metric, so that Windows will prefer its addresses. However, this means that you will not be able to access the devices on your local network that have the same IP addresses as the devices on the VPN network. For example, if both networks have a server with the IP address 192.168.1.202, you will only be able to access the server on the VPN network, and not the one on your local network.
Solution
The solution is to change the metrics of your network adapters manually, so that you can choose which one to use for resolving IP addresses. You can do this using PowerShell commands that modify the interface metric property of the network adapters. You can create two PowerShell scripts that will set the metrics of the two adapters, where one sets the VPN as the lower metric, and the other does the opposite. This will allow you to switch between the two adapters as needed, without having to disconnect and reconnect the VPN. The PowerShell commands you need to use are:
- Get-NetIPInterface: This command will list all the network interfaces on your computer, along with their interface index and interface metric values. You can use this command to identify the network adapters you want to change.
- Set-NetIPInterface: This command will change the properties of a network interface, such as the interface metric. You can use this command to set the metrics of the network adapters to the values you want.
For example, suppose you have two network adapters: one for the local network with the interface index 3 and the interface metric 10, and one for the VPN network with the interface index 4 and the interface metric 5. To set the VPN as the lower metric, you can use the following PowerShell command:
Set-NetIPInterface -InterfaceIndex 3 -InterfaceMetric 15
This will change the interface metric of the local network adapter to 15, which is higher than the VPN network adapter’s metric of 5. This means that Windows will prefer the VPN network adapter for resolving IP addresses. To set the local network as the lower metric, you can use the following PowerShell command:
Set-NetIPInterface -InterfaceIndex 3 -InterfaceMetric 5
This will change the interface metric of the local network adapter to 5, which is lower than the VPN network adapter’s metric of 15. This means that Windows will prefer the local network adapter for resolving IP addresses.
You can save these commands as two separate PowerShell scripts, and run them as needed to switch between the two network adapters. Alternatively, you can create a single PowerShell script that will toggle the metrics of the two network adapters, depending on their current values. For example, you can use the following PowerShell script:
$local = Get-NetIPInterface -InterfaceIndex 3
$vpn = Get-NetIPInterface -InterfaceIndex 4
if ($local.InterfaceMetric -lt $vpn.InterfaceMetric) {
Set-NetIPInterface -InterfaceIndex 3 -InterfaceMetric 15
} else {
Set-NetIPInterface -InterfaceIndex 3 -InterfaceMetric 5
}
This script will check the current metrics of the two network adapters, and change them accordingly. If the local network adapter has a lower metric than the VPN network adapter, it will set the local network adapter’s metric to 15, and vice versa. You can run this script as needed to toggle between the two network adapters.
Summary
In this article, we have learned how to connect to clients with different IP ranges on OpenVPN using PowerShell commands to change the metrics of network adapters. This can help you avoid conflicts and access the resources you need on both networks. We have also answered some frequently asked questions about network adapters, IP addresses, subnets, and metrics.
Disclaimer: This article is for informational purposes only and does not constitute professional advice. You should always consult your IT administrator or a qualified expert before making any changes to your network settings. We are not responsible for any damages or losses that may result from following the instructions in this article.