Skip to content

Instantly share code, notes, and snippets.

@jimfrenette
Last active December 20, 2023 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jimfrenette/21c7f19bc12c94c60628bebbf943a974 to your computer and use it in GitHub Desktop.
Save jimfrenette/21c7f19bc12c94c60628bebbf943a974 to your computer and use it in GitHub Desktop.
For WSL2 Linux: get IP from `/etc/resolv.conf` and update `/etc/hosts`. A use case for this is when you want to target localhost on Windows from Linux on WSL2. https://jimfrenette.com/2020/07/wsl2-windows-terminal/
#!/usr/bin/env bash
# the directory that contains this script
base=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
function removehost() {
if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
then
echo "$HOSTNAME Found in your /etc/hosts, Removing now...";
sudo sed -i".bak" "/$HOSTNAME/d" /etc/hosts
else
echo "$HOSTNAME was not found in your /etc/hosts";
fi
addhost
}
#add new ip host pair to `/etc/hosts`
function addhost() {
if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
then
echo "$HOSTNAME already exists:";
echo $(grep $HOSTNAME /etc/hosts);
else
echo "Adding $HOSTNAME to your /etc/hosts";
printf "%s\t%s\n" "$IP" "$HOSTNAME" | sudo tee -a /etc/hosts > /dev/null;
if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
then
echo "$HOSTNAME was added succesfully:";
echo $(grep $HOSTNAME /etc/hosts);
else
echo "Failed to Add $HOSTNAME, Try again!";
fi
fi
}
# windows localhost hostname to add/update in `/etc/hosts`
if [ "$1" != "" ]; then
HOSTNAME=$1
else
echo "Enter hostname to add or update, e.g., win.localhost"
read -p 'HOSTNAME: ' HOSTNAME
fi
# get IP from nameserver in `/etc/resolv.conf`
# from `https://rimuhosting.com/knowledgebase/linux/misc/checking-your-resolv.conf-file`
ns=$(cat /etc/resolv.conf | grep -v '^#' | grep nameserver | awk '{print $2}')
for i in $ns; do ptr=$(host $i | sed 's/Name: //' | sed 's/ .*//g' | head -n 1)
echo $i
IP=$i
removehost
# if dig @$i -t ns rimuhosting.com |grep -qai 'rimuhosting'; then
# echo $i $ptr OK;
# else
# echo $i $ptr failed;
# fi;
done
@dhulke
Copy link

dhulke commented Oct 9, 2020

I used your script as a basis for mine. I tried making it shorter and less verbose but works just the same. I also added the HOSTS_FILE variable to ease testing.

#!/usr/bin/env sh

HOSTS_FILE=/etc/hosts

hostname="${1:-wslhost}"
nameserver_ip=$(grep -v '^#' /etc/resolv.conf | grep nameserver | awk '{print $2}')
new_hosts_entry="$nameserver_ip\t$hostname"
current_hosts_entry="$(grep $hostname $HOSTS_FILE)"

is_new_hosts_entry_installed () [ -n "$(grep -P $new_hosts_entry $HOSTS_FILE)" ]


if is_new_hosts_entry_installed
then
    echo "Current entry: $current_hosts_entry"
else

    if [ -n "$current_hosts_entry" ]
    then
        sudo sed -i".bak" "/$hostname/d" "$HOSTS_FILE"
        echo "Current entry: $current_hosts_entry"
    fi
    echo "New entry: $new_hosts_entry"

    echo "$new_hosts_entry" | sudo tee -a "$HOSTS_FILE" > /dev/null

    if ! is_new_hosts_entry_installed
    then
        echo "Operation failed"
    fi

fi

@jimfrenette
Copy link
Author

Thanks @dhulke

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment