Skip to content

Instantly share code, notes, and snippets.

@sourabhv
Created August 18, 2022 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sourabhv/c0a2ae51ebfc1ede6a26d215f1747acf to your computer and use it in GitHub Desktop.
Save sourabhv/c0a2ae51ebfc1ede6a26d215f1747acf to your computer and use it in GitHub Desktop.
Bash script to fix internet access in WSL2 via powershell when connected to VPN which changes network interface adapter (like Cisco AnyConnect)
#!/bin/bash
# MIT License
# -----------------------------------------------------------------------------
# Copyright 2022 Sourabh Verma
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# -----------------------------------------------------------------------------
TMP=`mktemp`
trap ctrlC INT
removeTempFiles() {
rm -f $TMP
}
ctrlC() {
echo
echo "Trapped Ctrl-C, removing temporary files"
removeTempFiles
stty sane
}
WUSR=$(powershell.exe whoami | powershell.exe whoami | cut -f2- -d '\' | sed 's/\r$//')
P1="/mnt/c/users/$WUSR/pwsh/netfix.ps1"
P2="/mnt/c/users/$WUSR/pwsh/cisconetfix.ps1"
WP1="C:\Users\\$WUSR\pwsh\netfix.ps1"
WP2="C:\Users\\$WUSR\pwsh\cisconetfix.ps1"
C1="start-process powershell -verb runas -ArgumentList '-F \"$WP2\"'"
C2="Get-NetAdapter | Where-Object {\$_.InterfaceDescription -Match \"Cisco AnyConnect\"} | Set-NetIPInterface -InterfaceMetric 4000"
C3="Get-NetIPInterface -InterfaceAlias \"vEthernet (WSL)\" | Set-NetIPInterface -InterfaceMetric 1"
if [ ! -f "$P1" ]; then
echo "$P1 does not exist. Creating..."
mkdir -p "$(dirname "$P1")"
echo $C1 > "$P1"
fi
if [ ! -f "$P2" ]; then
echo "$P2 does not exist. Creating..."
mkdir -p "$(dirname "$P2")"
echo $C2 > "$P2"
echo $C3 >> "$P2"
fi
echo 'Mapping new network interfaces.'
echo '-------------------------------'
echo '(Enter your credentials in UAC Prompt)'
echo
echo
powershell.exe -File "$WP1"
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.exe -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
(set -x; sudo mv $TMP /etc/resolv.conf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment