Skip to content

Instantly share code, notes, and snippets.

@ppcamp
Last active May 30, 2024 15:47
Show Gist options
  • Save ppcamp/67ae954a154a0e7d6833f273b22217e5 to your computer and use it in GitHub Desktop.
Save ppcamp/67ae954a154a0e7d6833f273b22217e5 to your computer and use it in GitHub Desktop.
Patch and steps to get Cisco DNS work in wsl2
#
# ██████╗██╗███████╗ ██████╗ ██████╗
# ██╔════╝██║██╔════╝██╔════╝██╔═══██╗
# ██║ ██║███████╗██║ ██║ ██║
# ██║ ██║╚════██║██║ ██║ ██║
# ╚██████╗██║███████║╚██████╗╚██████╔╝
# ╚═════╝╚═╝╚══════╝ ╚═════╝ ╚═════╝
#
# ██████╗ █████╗ ████████╗ ██████╗██╗ ██╗██╗███╗ ██╗ ██████╗
# ██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██║ ██║██║████╗ ██║██╔════╝
# ██████╔╝███████║ ██║ ██║ ███████║██║██╔██╗ ██║██║ ███╗
# ██╔═══╝ ██╔══██║ ██║ ██║ ██╔══██║██║██║╚██╗██║██║ ██║
# ██║ ██║ ██║ ██║ ╚██████╗██║ ██║██║██║ ╚████║╚██████╔╝
# ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝ ╚═════╝
################################################################################
#
# Fix iface metric when starting cisco VPN
#
################################################################################
# Steps
# -----
# 1. First of all, you'll need to get the proper dns server for you, and to do
# that, You can use nslookup in powersheel (after connected to VPN), or the
# cmd at step [2]
#
# 2. After this, create/change the /etc/wsl.conf, disabling the resolv conf gen:
# /etc/wsl.conf
# [network]
# generateResolvConf = false
#
# 3. Then, you need to configure the /etc/resolv.conf file in your WSL
# distribution.
#
# /etc/resolv.conf
# # This file was automatically generated by WSL. To stop automatic
# # generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
# nameserver 172.19.240.1
#
# 3. Then, after reboot and connect to VPN, use this script everytime.
#
# 4. Check your connection using ping 8.8.8.8
#
#
# See
# ---
# This script is based on
# https://stackoverflow.com/questions/66444822/wsl-2-dns-is-not-working-with-vpn-connection-on-win-10
#
#
# Note
# ----
# In my case, the cisco adapter in the Windows Store, doesn't work anymore.
# Sometimes it disconnects suddenly. This is why I need to use the original VPN
# client for this.
#
# NOTE too that you'll need to enable the script execution for powershell
#
# >> Get-ExecutionPolicy
# Restricted
# >> Set-ExecutionPolicy RemoteSigned
# >> Get-ExecutionPolicy
# RemoteSigned
clear
cd ~
Write-Host "[1] Starting patching..."
Write-Host "[2] Getting DHCP server addr"
$DNS=(Get-NetAdapter | Where-Object InterfaceDescription -like "Cisco AnyConnect*" | Get-DnsClientServerAddress).ServerAddresses
Write-Host "[3] Changing iface metric"
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000
Write-Host "[4] Rebooting wsl"
wsl --shutdown
Write-Host "[5] Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment