Had this odd issue over the past week that I’d been trying to figure out once connected to company VPN via Cisco AnyConnect.
This annoyed me enough that I wrote up a PowerShell Script to automatically handle the mode switching when on and off VPN.
Symptoms:
- Curl didn’t work
- Ping didn’t work
There were loads of solutions online but this one worked the best for me and required no modification of the Windows Network Setting – I don’t have administrative rights on my machine and wanted an option that didn’t require getting help from IT.
Automatic Script to Enable AnyConnect Mode for WSL Ubuntu:
- Create “enableAnyConnect.ps1” somewhere on computer
- Add the following code to the file
- Execute the file via PowerShell window using ./enableAnyConnect.ps1
wsl -d ubuntu bash -c "cat /etc/resolv.conf &&\
sudo cp /etc/resolv.conf /etc/resolv.conf.bak &&\
sudo rm -f /etc/wsl.conf &&\
echo '[network]' | sudo tee /etc/wsl.conf &&\
echo 'generateResolvConf = false' | sudo tee -a /etc/wsl.conf"
wsl --terminate ubuntu
wsl -d ubuntu bash -c "sudo cp --remove-destination /etc/resolv.conf.bak /etc/resolv.conf &&\
sudo sed -i '/nameserver/s/^/#/' /etc/resolv.conf"
$ciscoAnyconnectAdapter = Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"}
$output = Get-DnsClientServerAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -Match $ciscoAnyconnectAdapter.InterfaceAlias}
foreach($serverAddress in $output.ServerAddresses)
{
wsl -d ubuntu bash -c "echo 'nameserver $serverAddress' | sudo tee -a /etc/resolv.conf"
}
wsl -d ubuntu bash -c "curl https://www.google.com"
Automatic Script to Disable AnyConnect Mode for WSL Ubuntu:
- Create “disableAnyConnect.ps1” somewhere on computer
- Add the following code to the file
- Execute the file via PowerShell window using ./disableAnyConnect.ps1
wsl -d ubuntu bash -c "sudo rm -f /etc/wsl.conf"
wsl --terminate ubuntu
wsl -d ubuntu bash -c "curl https://www.google.com"
Drawbacks
- If the IP Address for the DNS server changes (reboot or needing to reauthenticate with AnyConnect) you’ll need to repeat just this section of the guide
- My personal steps are to execute the disable script first then execute the enable script after
References: