Skip to content

Instantly share code, notes, and snippets.

@AndreSteenveld
Forked from mpneuried/update-hosts.sh
Last active January 19, 2020 09:07
Show Gist options
  • Save AndreSteenveld/df31e75a62d8c20274b0199ebf05e364 to your computer and use it in GitHub Desktop.
Save AndreSteenveld/df31e75a62d8c20274b0199ebf05e364 to your computer and use it in GitHub Desktop.
A small shell script that will add and remove lines from the hosts file. Originally created by Claus Witt, http://clauswitt.com/319.html.
#! /bin/sh
# @author: Claus Witt
# http://clauswitt.com/319.html
# Adding or Removing Items to hosts file
# Use -h flag for help
DEFAULT_IP=127.0.0.1
IP=${3:-$DEFAULT_IP}
HOST_FILE=${4:/etc/hosts}
case "$1" in
add)
sed -ie "\|\s{1,\}$2\$|d" $HOST_FILE
echo "$IP $2" >> $HOST_FILE
;;
remove)
sed -ie "\|^$IP $2\$|d" $HOST_FILE
;;
*)
echo "Usage: "
echo "hosts.sh [add|remove] [hostname] [ip] [host file]"
echo
echo "Ip defaults to 127.0.0.1"
echo "Host file defaults to /etc/hosts"
echo "Examples:"
echo "hosts.sh add testing.com"
echo "hosts.sh remove testing.com 192.168.1.1"
echo "hosts.sh add testing.com 192.168.1.1 ~/hosts"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment