Skip to content

Instantly share code, notes, and snippets.

@alejandromav
Created March 23, 2020 10:24
Show Gist options
  • Save alejandromav/ea4dd54b2c8f0e81eef56d2b75c2ba60 to your computer and use it in GitHub Desktop.
Save alejandromav/ea4dd54b2c8f0e81eef56d2b75c2ba60 to your computer and use it in GitHub Desktop.
Fix WSL DNS resolving for VPNs
#!/bin/bash
tmp=`mktemp`
powershell="/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"
trap ctrlC INT
removeTempFiles() {
rm -f $tmp
}
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'
nameservers=$($powershell \
-Command "Get-DnsClientServerAddress -AddressFamily ipv4 | Select-Object -ExpandProperty ServerAddresses" \
)
for i in $nameservers; do
echo nameserver $i
done
tail -n+2 /etc/resolv.conf | grep -v '^nameserver'
} | tr -d '\r' | tee $tmp
(set -x; sudo cp -i $tmp /etc/resolv.conf)
removeTempFiles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment