Skip to content

Instantly share code, notes, and snippets.

@bastaramus
Created January 19, 2021 14:48
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bastaramus/3d5f1326835fe111d5ea9684c41a6675 to your computer and use it in GitHub Desktop.
Powershell script to fix internet connection issue with WSL2 and CheckPoint VPN
[IPAddress]$IP_wsl = (Get-NetIPAddress -InterfaceAlias "vEthernet (WSL)" -AddressFamily "IPv4" | Select-Object IPAddress).ipaddress
$PrefixLength_wsl = (Get-NetIPAddress -InterfaceAlias "vEthernet (WSL)" -AddressFamily "IPv4" | Select-Object PrefixLength).prefixlength
$idx_vpn = (Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Check Point"} | Select-Object ifIndex).ifIndex
Function CIDRToNetMask {
[CmdletBinding()]
Param(
[ValidateRange(0,32)]
[int16]$PrefixLength=0
)
$bitString=('1' * $PrefixLength).PadRight(32,'0')
$strBuilder=New-Object -TypeName Text.StringBuilder
for($i=0;$i -lt 32;$i+=8){
$8bitString=$bitString.Substring($i,8)
[void]$strBuilder.Append("$([Convert]::ToInt32($8bitString,2)).")
}
return $strBuilder.ToString().TrimEnd('.')
}
[IPAddress]$NetMask = CIDRToNetMask($PrefixLength_wsl)
$NetAddress_wsl = [ipaddress]($IP_wsl.Address -band $NetMask.Address)
$routes = Get-NetRoute -AddressFamily IPv4 -InterfaceIndex $idx_vpn
foreach ($route in $routes) {
$dst_address = [ipaddress]$route.DestinationPrefix.split('/')[0]
$netaddress_dst = [ipaddress]($dst_address.Address -band $NetMask.Address)
if ($netaddress_dst.IPAddressToString -eq $NetAddress_wsl.IPAddressToString) {
Remove-NetRoute -InputObject $route -Confirm:$false
}
}
@bastaramus
Copy link
Author

CheckPoint_VPN_fix_wsl_routes

Powershell script to fix internet connection issue with WSL2 and CheckPoint VPN. It rewrites the route table in your Windows host machine.

How To

Just run the script in the Windows Powershell as an Administrator:

.\vpn_fix_wsl_routes.ps1

You need to run it each time when you was connected by the CheckPoint VPN.

If it dosn't work, make sure you run it as Administrator. By default, we suppose that the Name of your WSL2 Network Interface is vEthernet (WSL) and InterfaceDescription of your CheckPoint Interface has "Check Point" words.

@PavelFranta
Copy link

PS C:\> .\vpn_fix_wsl_routes.ps1
Get-NetRoute : Cannot validate argument on parameter 'InterfaceIndex'. The argument is null. Provide a valid value for
the argument, and then try running the command again.
At C:\vpn_fix_wsl_routes.ps1:27 char:60
+ $routes = Get-NetRoute -AddressFamily IPv4 -InterfaceIndex $idx_vpn
+                                                            ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-NetRoute], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-NetRoute

@twine003
Copy link

I'm having the same issue with InterfaceIndex

@kristofersokk
Copy link

Worked for me. Thank you so much!

@carlosbustillordguez
Copy link

Worked for me!! Thanks!

@allanmedeiros71
Copy link

It works for me! Thanks.
I needed to change the execution policy

PS C:\Users\LINQ> Get-ExecutionPolicy
Restricted

PS C:\Users\LINQ> Set-ExecutionPolicy RemoteSigned
PS C:\Users\LINQ> Get-ExecutionPolicy
RemoteSigned

@PeterShaws
Copy link

Confirming that it works for me as well.

If you don't want to change your execution policy, you can run this command before the script:

PS> Unblock-File -Path path\to\script\vpn_fix_wsl_routes.ps1

Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment