Last active
January 3, 2025 02:37
-
-
Save jarlef/caa84c24f9ec22856b25 to your computer and use it in GitHub Desktop.
Shell script to update hosts file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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