Skip to content

Instantly share code, notes, and snippets.

@stvvt
Forked from irazasyed/manage-etc-hosts.sh
Last active September 26, 2018 05:00

Revisions

  1. stvvt revised this gist May 14, 2016. 1 changed file with 29 additions and 27 deletions.
    56 changes: 29 additions & 27 deletions manage-etc-hosts.sh
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -1,41 +1,43 @@
    #!/usr/bin/env bash

    set -eu

    # PATH TO YOUR HOSTS FILE
    : ${ETC_HOSTS="/etc/hosts"}

    # DEFAULT IP FOR HOSTNAME
    : ${IP="127.0.0.1"}

    # Hostname to add/remove.
    HOSTNAME=$1
    DEFAULT_IP="127.0.0.1"

    VERBOSE=false

    function removehost() {
    if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
    then
    [ ${VERBOSE} == true ] && echo "$HOSTNAME Found in your $ETC_HOSTS, Removing now..."
    sudo sed -i".bak" "/$HOSTNAME/d" $ETC_HOSTS
    function remove() {
    local HOSTNAME=$1
    local HOST_REGEX="\(\s\+\)${HOSTNAME}\s*$"
    local HOST_LINE="$(grep -e "${HOST_REGEX}" ${ETC_HOSTS})"

    if [ -n "${HOST_LINE}" ]; then
    [ ${VERBOSE} == true ] && echo "${HOSTNAME} Found in your ${ETC_HOSTS}, Removing now..."
    sed -i -e "s/${HOST_REGEX}/\1/g" -e "/^[^#][0-9\.]\+\s\+$/d" ${ETC_HOSTS}
    else
    [ ${VERBOSE} == true ] && echo "$HOSTNAME was not found in your $ETC_HOSTS";
    [ ${VERBOSE} == true ] && echo "${HOSTNAME} was not found in your ${ETC_HOSTS}";
    fi
    }

    function addhost() {
    HOSTNAME=$1
    HOSTS_LINE="$IP\t$HOSTNAME"
    if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
    then
    [ ${VERBOSE} == true ] && echo "$HOSTNAME already exists : $(grep $HOSTNAME $ETC_HOSTS)"
    else
    echo "Adding $HOSTNAME to your $ETC_HOSTS";
    sudo -- sh -c -e "echo '$HOSTS_LINE' >> /etc/hosts";

    if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
    then
    [ ${VERBOSE} == true ] && echo "$HOSTNAME was added succesfully \n $(grep $HOSTNAME /etc/hosts)";
    else
    [ ${VERBOSE} == true ] && echo "Failed to Add $HOSTNAME, Try again!";
    fi
    function add() {
    local HOSTNAME=$1
    local IP=${2:-${DEFAULT_IP}}

    local HOST_REGEX="\(\s\+\)${HOSTNAME}\s*$"
    local HOST_LINE="$(grep -e "${HOST_REGEX}" ${ETC_HOSTS})"

    if [ -n "${HOST_LINE}" ]; then
    [ ${VERBOSE} == true ] && echo "${HOSTNAME} already exists : ${HOST_LINE}"
    else
    [ ${VERBOSE} == true ] && echo "Adding ${HOSTNAME} to your ${ETC_HOSTS}";
    echo -e "${IP}\t${HOSTNAME}" >> ${ETC_HOSTS}
    [ ${VERBOSE} == true ] && echo -e "${HOSTNAME} was added succesfully \n ${HOST_LINE}";
    fi
    }
    }

    # Execute
    $@
  2. stvvt revised this gist May 14, 2016. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions manage-etc-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,23 @@
    #!/bin/sh
    #!/usr/bin/env bash

    # PATH TO YOUR HOSTS FILE
    ETC_HOSTS=/etc/hosts
    : ${ETC_HOSTS="/etc/hosts"}

    # DEFAULT IP FOR HOSTNAME
    IP="127.0.0.1"
    : ${IP="127.0.0.1"}

    # Hostname to add/remove.
    HOSTNAME=$1

    VERBOSE=false

    function removehost() {
    if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
    then
    echo "$HOSTNAME Found in your $ETC_HOSTS, Removing now...";
    [ ${VERBOSE} == true ] && 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";
    [ ${VERBOSE} == true ] && echo "$HOSTNAME was not found in your $ETC_HOSTS";
    fi
    }

    @@ -24,16 +26,16 @@ function addhost() {
    HOSTS_LINE="$IP\t$HOSTNAME"
    if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
    then
    echo "$HOSTNAME already exists : $(grep $HOSTNAME $ETC_HOSTS)"
    [ ${VERBOSE} == true ] && echo "$HOSTNAME already exists : $(grep $HOSTNAME $ETC_HOSTS)"
    else
    echo "Adding $HOSTNAME to your $ETC_HOSTS";
    sudo -- sh -c -e "echo '$HOSTS_LINE' >> /etc/hosts";

    if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
    then
    echo "$HOSTNAME was added succesfully \n $(grep $HOSTNAME /etc/hosts)";
    [ ${VERBOSE} == true ] && echo "$HOSTNAME was added succesfully \n $(grep $HOSTNAME /etc/hosts)";
    else
    echo "Failed to Add $HOSTNAME, Try again!";
    [ ${VERBOSE} == true ] && echo "Failed to Add $HOSTNAME, Try again!";
    fi
    fi
    }
  3. @irazasyed irazasyed created this gist Mar 7, 2015.
    39 changes: 39 additions & 0 deletions manage-etc-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/bin/sh

    # PATH TO YOUR HOSTS FILE
    ETC_HOSTS=/etc/hosts

    # DEFAULT IP FOR HOSTNAME
    IP="127.0.0.1"

    # Hostname to add/remove.
    HOSTNAME=$1

    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
    }

    function addhost() {
    HOSTNAME=$1
    HOSTS_LINE="$IP\t$HOSTNAME"
    if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
    then
    echo "$HOSTNAME already exists : $(grep $HOSTNAME $ETC_HOSTS)"
    else
    echo "Adding $HOSTNAME to your $ETC_HOSTS";
    sudo -- sh -c -e "echo '$HOSTS_LINE' >> /etc/hosts";

    if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
    then
    echo "$HOSTNAME was added succesfully \n $(grep $HOSTNAME /etc/hosts)";
    else
    echo "Failed to Add $HOSTNAME, Try again!";
    fi
    fi
    }