Difference between revisions of "Wsl2 vpn fix route"
Jump to navigation
Jump to search
(Created page with "``` Get-NetIPInterface -InterfaceAlias "vEthernet (WSL)" | Set-NetIPInterface -InterfaceMetric 1 Get-NetAdapter | Where-Object { $_.InterfaceDescription -like "PANGP*" } | Set...") |
|||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| + | # Best for Hyper-V firewall vswtich | ||
| + | ``` | ||
| + | $interfaceAlias = "vEthernet (WSL (Hyper-V firewall))" | ||
| + | $interfaceName = "usb" | ||
| + | $vpnInterfaceRegex = "PANGP*" | ||
| + | $vpnDNS = "10.x.x.y" | ||
| + | $ipOfDefaultGatewayIPRouteToDelete = "192.168.1.1" | ||
| + | |||
| + | $ErrorActionPreference = "Stop" | ||
| + | |||
| + | if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { | ||
| + | $arguments = "& '" + $myInvocation.MyCommand.Definition + "'" | ||
| + | Start-Process powershell -Verb runAs -ArgumentList $arguments | ||
| + | exit | ||
| + | } | ||
| + | |||
| + | |||
| + | Get-NetIPInterface -InterfaceAlias $interfaceAlias | Set-NetIPInterface -InterfaceMetric 1 | ||
| + | Get-NetAdapter | Where-Object { $_.InterfaceDescription -like $vpnInterfaceRegex } | Set-NetIPInterface -InterfaceMetric 4000 | ||
| + | |||
| + | Set-DnsClientServerAddress -InterfaceAlias $interfaceName -ServerAddresses ($vpnDNS) | ||
| + | route delete 0.0.0.0 MASK 0.0.0.0 $ipOfDefaultGatewayIPRouteToDelete | ||
| + | |||
| + | ``` | ||
| + | |||
| + | # Other Options Brief | ||
| + | |||
| + | |||
| + | |||
``` | ``` | ||
Get-NetIPInterface -InterfaceAlias "vEthernet (WSL)" | Set-NetIPInterface -InterfaceMetric 1 | Get-NetIPInterface -InterfaceAlias "vEthernet (WSL)" | Set-NetIPInterface -InterfaceMetric 1 | ||
Get-NetAdapter | Where-Object { $_.InterfaceDescription -like "PANGP*" } | Set-NetIPInterface -InterfaceMetric 4000 | Get-NetAdapter | Where-Object { $_.InterfaceDescription -like "PANGP*" } | Set-NetIPInterface -InterfaceMetric 4000 | ||
# Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 4000 | # Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 4000 | ||
| + | ``` | ||
| + | |||
| + | Set routes to default gateway | ||
| + | ``` | ||
| + | route add 0.0.0.0 MASK 0.0.0.0 10.x.x.1 | ||
| + | route add 172.24.192.0 MASK 255.255.240.0 10.x.x.1 | ||
| + | ``` | ||
| + | |||
| + | ``` | ||
| + | if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) | ||
| + | { | ||
| + | # Re-launch the script with elevated privileges | ||
| + | $arguments = "& '" + $myInvocation.MyCommand.Definition + "'" | ||
| + | Start-Process powershell -Verb runAs -ArgumentList $arguments | ||
| + | exit | ||
| + | } | ||
| + | |||
| + | Get-NetIPInterface -InterfaceAlias "vEthernet (WSL)" | Set-NetIPInterface -InterfaceMetric 1 | ||
| + | Get-NetAdapter | Where-Object { $_.InterfaceDescription -like "PANGP*" } | Set-NetIPInterface -InterfaceMetric 4000 | ||
| + | # Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 4000 | ||
| + | ``` | ||
| + | |||
| + | |||
| + | # Messy Notes | ||
| + | ``` | ||
| + | $interface = Get-NetIPConfiguration | Where-Object { $_.InterfaceAlias -eq "vEthernet (WSL)" } | ||
| + | $prefixLength = $interface.IPv4Address | Select-Object -ExpandProperty PrefixLength | ||
| + | $binaryMask = ('1' * $prefixLength).PadRight(32, '0'); $subnetMask = [convert]::ToInt32($binaryMask.Substring(0, 8), 2).ToString() + '.' + [convert]::ToInt32($binaryMask.Substring(8, 8), 2).ToString() + '.' + [convert]::ToInt32($binaryMask.Substring(16, 8), 2).ToString() + '.' + [convert]::ToInt32($binaryMask.Substring(24, 8), 2).ToString(); Write-Output $subnetMask | ||
| + | |||
| + | route add $interface.IPv4Address.IPAddress MASK $subnetMask 10.x.x.1 | ||
``` | ``` | ||
Latest revision as of 16:27, 23 August 2024
Best for Hyper-V firewall vswtich
$interfaceAlias = "vEthernet (WSL (Hyper-V firewall))"
$interfaceName = "usb"
$vpnInterfaceRegex = "PANGP*"
$vpnDNS = "10.x.x.y"
$ipOfDefaultGatewayIPRouteToDelete = "192.168.1.1"
$ErrorActionPreference = "Stop"
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
$arguments = "& '" + $myInvocation.MyCommand.Definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
exit
}
Get-NetIPInterface -InterfaceAlias $interfaceAlias | Set-NetIPInterface -InterfaceMetric 1
Get-NetAdapter | Where-Object { $_.InterfaceDescription -like $vpnInterfaceRegex } | Set-NetIPInterface -InterfaceMetric 4000
Set-DnsClientServerAddress -InterfaceAlias $interfaceName -ServerAddresses ($vpnDNS)
route delete 0.0.0.0 MASK 0.0.0.0 $ipOfDefaultGatewayIPRouteToDelete
Other Options Brief
Get-NetIPInterface -InterfaceAlias "vEthernet (WSL)" | Set-NetIPInterface -InterfaceMetric 1
Get-NetAdapter | Where-Object { $_.InterfaceDescription -like "PANGP*" } | Set-NetIPInterface -InterfaceMetric 4000
# Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 4000
Set routes to default gateway
route add 0.0.0.0 MASK 0.0.0.0 10.x.x.1 route add 172.24.192.0 MASK 255.255.240.0 10.x.x.1
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
# Re-launch the script with elevated privileges
$arguments = "& '" + $myInvocation.MyCommand.Definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
exit
}
Get-NetIPInterface -InterfaceAlias "vEthernet (WSL)" | Set-NetIPInterface -InterfaceMetric 1
Get-NetAdapter | Where-Object { $_.InterfaceDescription -like "PANGP*" } | Set-NetIPInterface -InterfaceMetric 4000
# Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 4000
Messy Notes
$interface = Get-NetIPConfiguration | Where-Object { $_.InterfaceAlias -eq "vEthernet (WSL)" }
$prefixLength = $interface.IPv4Address | Select-Object -ExpandProperty PrefixLength
$binaryMask = ('1' * $prefixLength).PadRight(32, '0'); $subnetMask = [convert]::ToInt32($binaryMask.Substring(0, 8), 2).ToString() + '.' + [convert]::ToInt32($binaryMask.Substring(8, 8), 2).ToString() + '.' + [convert]::ToInt32($binaryMask.Substring(16, 8), 2).ToString() + '.' + [convert]::ToInt32($binaryMask.Substring(24, 8), 2).ToString(); Write-Output $subnetMask
route add $interface.IPv4Address.IPAddress MASK $subnetMask 10.x.x.1