Skip to content

Instantly share code, notes, and snippets.

@dylan2intel
Last active December 30, 2021 04:31
Show Gist options
  • Save dylan2intel/3766de9dcfb2090787a9d5bab3417182 to your computer and use it in GitHub Desktop.
Save dylan2intel/3766de9dcfb2090787a9d5bab3417182 to your computer and use it in GitHub Desktop.
WSL2 Cisco AnyConnect VPN Client Networking Connection Issue Workaround

How to use the above files?

Step 1: Copy files updateInterfaceMetric.ps1, updateDns.ps1, autodns.sh to your Windows path %HOMEPATH%\WSL2\scripts, if not exists, create this folder.

Step 2: Win+R Run taskschd.msc will open "Task Schedulder" application, import two tasks with files UpdateInterfaceMetric.xml, updateDns.xml one by one.

#!/bin/bash
#Update DNS in file /etc/resolv.conf based on network connection status (up or down)
POWERSHELL_CMD='/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe'
# Get interface names of vpn,ethernet,wifi which are up status.
GET_INTERFACE_NAMES="Get-NetAdapter \
| Where-Object -Property InterfaceDescription -Match \"Cisco AnyConnect|Gigabit|Wi.*Fi\" \
| Where-Object -Property Status -eq \"Up\" \
| Select-Object -ExpandProperty Name"
#echo "$GET_INTERFACE_NAMES"
INTERFACE_NAMES=$($POWERSHELL_CMD -Command "$GET_INTERFACE_NAMES")
COMBILED_NAMES=$(echo -e "$INTERFACE_NAMES" |tr -d '\r'|tr '\n' '|')
#echo "$COMBILED_NAMES"
# Get dns names by interface names
GET_DNS_NAMES="Get-DnsClientServerAddress -AddressFamily IPv4 \
| Where-Object -Property InterfaceAlias -Match \"$COMBILED_NAMES\" \
| Select-Object -ExpandProperty ServerAddresses"
#echo "$GET_DNS_NAMES"
DNS_NAMES=$($POWERSHELL_CMD -Command "$GET_DNS_NAMES")
echo -e "\n------------current /etc/resolv.conf------------"
if [ -L /etc/resolv.conf ]; then
unlink /etc/resolv.conf
else
cat /etc/resolv.conf
fi
#Update /etc/resolv.conf
{
for i in $DNS_NAMES; do
echo nameserver "$i"
done
} | tr -d '\r' | tee /etc/resolv.conf > /dev/null
echo -e "\n------------updated /etc/resolv.conf------------"
cat /etc/resolv.conf
# Update DNS of wsl based on networking status change(up or down)
# Event ids generated by Cisco AnyConnect when connection termination
#2039: VPN Established and Passing Data
#2061: Network Interface for the VPN has gone down
#2010: VPN Termination
#2041: The entire VPN connection has been re-established.
$distro="Ubuntu"
$user=((Get-WMIObject -ClassName Win32_ComputerSystem).Username).Split('\')[1]
wsl.exe -d $distro -u root /mnt/c/Users/$user/WSL2/scripts/autodns.sh
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-10-14T10:35:06.9051925</Date>
<Author>CCR\deyinche</Author>
<Description>Update DNS of wsl based on networking status change(up or down)</Description>
<URI>\WSL2\updateDns</URI>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Cisco AnyConnect Secure Mobility Client"&gt;&lt;Select Path="Cisco AnyConnect Secure Mobility Client"&gt;*[System[Provider[@Name='acvpnagent'] and (EventID=2039 or EventID=2010 or EventID=2061 or EventID=2041)]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-21-1757981266-725345543-1404487317-531068</UserId>
<LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>Powershell.exe</Command>
<Arguments>-WindowStyle Hidden -NonInteractive -ExecutionPolicy Bypass -File %HOMEPATH%\WSL2\scripts\updateDns.ps1</Arguments>
</Exec>
</Actions>
</Task>
# Update Cisco AnyConnect VPN Client InterfaceMetric to a higer value 6000
# Event ids generated by Cisco AnyConnect when connection established
# 2039: VPN Established and Passing Data
# 2041: The entire VPN connection has been re-established.
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-10-14T10:31:18.7113087</Date>
<Author>CCR\deyinche</Author>
<Description>Update Interface Metric of Cisco AnyConnect VPN Client to higer value 6000</Description>
<URI>\WSL2\UpdateInterfaceMetric</URI>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Cisco AnyConnect Secure Mobility Client"&gt;&lt;Select Path="Cisco AnyConnect Secure Mobility Client"&gt;*[System[Provider[@Name='acvpnagent'] and (EventID=2039 or EventID=2041)]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-21-1757981266-725345543-1404487317-531068</UserId>
<LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>Powershell.exe</Command>
<Arguments>-WindowStyle Hidden -NonInteractive -ExecutionPolicy Bypass -File %HOMEPATH%\WSL2\scripts\updateInterfaceMetric.ps1</Arguments>
</Exec>
</Actions>
</Task>
@fub2
Copy link

fub2 commented Dec 18, 2021

Very handy! Thank you for sharing!!

S-1-5-21-1757981266-725345543-1404487317-531068 - requires update for specific user
$distro="Ubuntu" Ubuntu-18.04 actually

@MostHated
Copy link

This worked perfectly. Huge thanks for sharing it.

P.S. Mine is and worked just fine as "Ubuntu", with no version number.

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