Skip to content

Instantly share code, notes, and snippets.

@MatMercer
Last active April 5, 2025 02:20
Show Gist options
  • Select an option

  • Save MatMercer/f7e25b9c8ce7ca40dd3b220346136d23 to your computer and use it in GitHub Desktop.

Select an option

Save MatMercer/f7e25b9c8ce7ca40dd3b220346136d23 to your computer and use it in GitHub Desktop.

Revisions

  1. MatMercer revised this gist Sep 20, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion wsl-vpn-fix.sh
    Original file line number Diff line number Diff line change
    @@ -80,5 +80,4 @@ cat $TMP_SCRIPT
    # run the network metrics fix script
    $POWERSHELL -command "$pcmd"


    removeTempFiles
  2. MatMercer revised this gist Sep 20, 2021. 1 changed file with 16 additions and 12 deletions.
    28 changes: 16 additions & 12 deletions wsl-vpn-fix.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    # #
    # Based on: #
    # https://askubuntu.com/a/15856 #
    # https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8ca6 #
    # https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8Aca6 #
    # https://gist.github.com/nfekete/7a277bf9e25e89e1c8bfb8b64dcc08ed #
    # https://github.com/microsoft/WSL/issues/4277#issuecomment-921087670 #
    # #
    @@ -41,14 +41,16 @@ chmod 777 $TMP_DIR
    trap ctrlC INT

    removeTempFiles() {
    rm -rf $TMP_DIR
    # powershell continues in the background, trying to use a file that was deleted. TODO: fix the racing condition
    # rm -rf $TMP_DIR
    true
    }

    ctrlC() {
    echo
    echo "Trapped Ctrl-C, removing temporary files"
    removeTempFiles
    stty sane
    echo
    echo "Trapped Ctrl-C, removing temporary files"
    removeTempFiles
    stty sane
    }

    echo "Current resolv.conf"
    @@ -60,14 +62,15 @@ echo "Creating new resolv.conf"
    echo "------------------------"

    {
    head -1 /etc/resolv.conf | grep '^#.*generated'
    for i in `$POWERSHELL -Command "Get-DnsClientServerAddress -AddressFamily ipv4 | Select-Object -ExpandProperty ServerAddresses"`; do
    echo nameserver $i
    done
    tail -n+2 /etc/resolv.conf | grep -v '^nameserver'
    head -1 /etc/resolv.conf | grep '^#.*generated'
    for i in `$POWERSHELL -Command "Get-DnsClientServerAddress -AddressFamily ipv4 | Select-Object -ExpandProperty ServerAddresses"`; do
    echo nameserver $i
    done
    tail -n+2 /etc/resolv.conf | grep -v '^nameserver'

    } | tr -d '\r' | tee $TMP_RESOLV

    (set -x; cp -i $TMP_RESOLV /etc/resolv.conf)
    (set -x; cp -i $TMP_RESOLV /etc/resolv.conf; set +x)

    echo
    echo "Fixing network metrics for cisco anyconnect"
    @@ -77,4 +80,5 @@ cat $TMP_SCRIPT
    # run the network metrics fix script
    $POWERSHELL -command "$pcmd"


    removeTempFiles
  3. MatMercer revised this gist Sep 20, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wsl-vpn-fix.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    # #
    # Based on: #
    # https://askubuntu.com/a/15856 #
    # https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8Aca6 #
    # https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8ca6 #
    # https://gist.github.com/nfekete/7a277bf9e25e89e1c8bfb8b64dcc08ed #
    # https://github.com/microsoft/WSL/issues/4277#issuecomment-921087670 #
    # #
  4. MatMercer created this gist Sep 20, 2021.
    80 changes: 80 additions & 0 deletions wsl-vpn-fix.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    #!/bin/bash
    #--------------------------------------------------------------------------------#
    # #
    # Fix WSL DNS resolution with Cisco AnyConnect #
    # #
    # ! Don't forget to set this configuration in /etc/wsl.conf: #
    # [network] #
    # generateResolvConf = false #
    # #
    # Based on: #
    # https://askubuntu.com/a/15856 #
    # https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8Aca6 #
    # https://gist.github.com/nfekete/7a277bf9e25e89e1c8bfb8b64dcc08ed #
    # https://github.com/microsoft/WSL/issues/4277#issuecomment-921087670 #
    # #
    # Enjoy, ~ Mateus Mercer <[email protected]> 2021 #
    # #
    #--------------------------------------------------------------------------------#

    if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root"
    exit 1
    fi

    TMP_DIR=`mktemp -d`
    TMP_SCRIPT="$TMP_DIR/network-metrics.ps1"
    TMP_RESOLV="$TMP_DIR/resolv.conf"
    POWERSHELL="powershell.exe"

    touch "$TMP_SCRIPT"
    # this starts the script as admin in powershell
    pcmd="Start-Process -FilePath $POWERSHELL -verb runas -ArgumentList $POWERSHELL,-noprofile,-executionpolicy,bypass,-file,$(wslpath -aw $TMP_SCRIPT)"

    # generate the fix script
    echo 'Get-NetIPInterface -InterfaceAlias "vEthernet (WSL)" | Set-NetIPInterface -InterfaceMetric 1
    Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000' > $TMP_SCRIPT

    # required to allow windows do read the folder contents
    chmod 777 $TMP_DIR

    trap ctrlC INT

    removeTempFiles() {
    rm -rf $TMP_DIR
    }

    ctrlC() {
    echo
    echo "Trapped Ctrl-C, removing temporary files"
    removeTempFiles
    stty sane
    }

    echo "Current resolv.conf"
    echo "-------------------"
    cat /etc/resolv.conf

    echo
    echo "Creating new resolv.conf"
    echo "------------------------"

    {
    head -1 /etc/resolv.conf | grep '^#.*generated'
    for i in `$POWERSHELL -Command "Get-DnsClientServerAddress -AddressFamily ipv4 | Select-Object -ExpandProperty ServerAddresses"`; do
    echo nameserver $i
    done
    tail -n+2 /etc/resolv.conf | grep -v '^nameserver'
    } | tr -d '\r' | tee $TMP_RESOLV

    (set -x; cp -i $TMP_RESOLV /etc/resolv.conf)

    echo
    echo "Fixing network metrics for cisco anyconnect"
    echo "-------------------------------------------"
    cat $TMP_SCRIPT

    # run the network metrics fix script
    $POWERSHELL -command "$pcmd"

    removeTempFiles