Skip to content

Instantly share code, notes, and snippets.

@jarlef
Last active October 4, 2023 10:27
Show Gist options
  • Save jarlef/caa84c24f9ec22856b25 to your computer and use it in GitHub Desktop.
Save jarlef/caa84c24f9ec22856b25 to your computer and use it in GitHub Desktop.
Shell script to update hosts file
#! /bin/sh
# @author: Jarle Friestad
# Based on script by 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}
case "$1" in
add)
echo "$IP $2" >> /etc/hosts
;;
remove)
sed -i "/\s$2/d" /etc/hosts
;;
*)
echo "Usage: update-hosts COMMAND [args..]"
echo
echo "Utility for adding or removing entries in hosts file"
echo
echo "Commands:"
echo " add update-hosts add [hostname] [ip]"
echo " remove update-hosts remove [hostname]"
echo
echo "Ip defaults to 127.0.0.1"
echo
echo "Examples:"
echo " update-hosts add testing.com 127.0.0.2"
echo " update-hosts remove testing.com"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment