Skip to content

Instantly share code, notes, and snippets.

@jimfrenette

jimfrenette/resolv.sh

Last active Oct 22, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@dhulke 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

This comment has been minimized.

Copy link
Owner Author

@jimfrenette jimfrenette commented Oct 22, 2020

Thanks @dhulke

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.