Created
August 28, 2020 16:27
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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