Skip to content

Instantly share code, notes, and snippets.

@mysiki
Created August 28, 2020 16:27
Show Gist options
  • Save mysiki/10a1c530152f319de7147b6fa99276bc to your computer and use it in GitHub Desktop.
Save mysiki/10a1c530152f319de7147b6fa99276bc to your computer and use it in GitHub Desktop.
Simple shell function for update windows hosts file with wsl IP. Can be put it in .bashrc
function wslhosts() {
vm_name="${1:-wsl}"
file_hosts="${2:-/mnt/c/Windows/System32/drivers/etc/hosts}"
wsl_ip=$(ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
mkdir -p "$(dirname "${file_hosts}")" && touch "${file_hosts}"
grep -q " ${vm_name}" "${file_hosts}"
if [ $? -eq 1 ]; then
echo "${wsl_ip} ${vm_name}" >> "${file_hosts}"
else
sed -i 's/.* ${vm_name}/${wsl_ip} ${vm_name}/g' "${file_hosts}"
fi
}
wslhosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment