Skip to content

Instantly share code, notes, and snippets.

@ValentineK
Forked from irazasyed/manage-etc-hosts.sh
Created September 4, 2018 07:09
Show Gist options
  • Save ValentineK/f2d8b73f6e41390ae3b11b4dd73fe220 to your computer and use it in GitHub Desktop.
Save ValentineK/f2d8b73f6e41390ae3b11b4dd73fe220 to your computer and use it in GitHub Desktop.
Bash Script to Manage /etc/hosts file for adding/removing hostnames.
#!/usr/bin/env bash
# Path to your hosts file
hostsFile="/etc/hosts"
# Default IP address for host
ip="${3:-127.0.0.1}"
# Hostname to add/remove.
hostname="$2"
yell() { echo "$0: $*" >&2; }
die() { yell "$*"; exit 111; }
try() { "$@" || die "cannot $*"; }
remove() {
if [ -n "$(grep -P "[[:space:]]$hostname" /etc/hosts)" ]; then
echo "$hostname found in $hostsFile. Removing now...";
try sudo sed -ie "/[[:space:]]$hostname/d" "$hostsFile";
else
yell "$hostname was not found in $hostsFile";
fi
}
add() {
if [ -n "$(grep -P "[[:space:]]$hostname" /etc/hosts)" ]; then
yell "$hostname, already exists: $(grep $hostname $hostsFile)";
else
echo "Adding $hostname to $hostsFile...";
try printf "%s\t%s\n" "$ip" "$hostname" | sudo tee -a "$hostsFile" > /dev/null;
if [ -n "$(grep $hostname /etc/hosts)" ]; then
echo "$hostname was added succesfully:";
echo "$(grep $hostname /etc/hosts)";
else
die "Failed to add $hostname";
fi
fi
}
$@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment