Permalink
Browse files

* Initial commit.

  • Loading branch information...
0 parents commit 4dc9b81b07fe6e37f3695b883ff0070a0394857d @perusio committed Aug 15, 2011
Showing with 192 additions and 0 deletions.
  1. +46 −0 README.md
  2. +75 −0 add-etc-hosts
  3. +64 −0 man/add-etc-hosts.8.ronn
  4. +6 −0 man/index.txt
  5. +1 −0 rm-etc-hosts
@@ -0,0 +1,46 @@
+# Add/Remove hostnames from the /etc/hosts file
+
+## Introduction
+
+This script was created for simplifying the addition/removal of
+hostnames to the `/etc/hosts` file. It's particularly useful if you're
+developing a site locally and want that site to have a "handle" that
+is related to the project name.
+
+For example if I'm developing a site that is for a widget maker that
+will use the domain mywidgetmaker.com I'll do:
+
+ add-etc-hosts mywidgetmaker.com.local
+
+Now I can enter `http://mywidgetmaker.com.local` on the address bar of the
+browser and it will show me the site that I'm developing.
+
+It's particularly useful for working with [Nginx](http://nginx.org),
+you just add the
+[`server_name`](http://wiki.nginx.org/HttpCoreModule#server_name)
+directive to your vhost configuration (server context), like this:
+
+ server_name mywidgetmaker.local;
+
+## Installation
+
+ 1. Clone the repo from []() or [download]() it.
+
+ 2. Put the scripts in a directory included in your `PATH` or define
+ aliases like this:
+
+ alias add-etc-hosts='/path/to/add-etc-hosts'
+ alias rm-etc-hosts='/path/to/rm-etc-hosts'
+
+ Source the new aliases, if you have them in a `~/.bash_aliases` file:
+
+ . ~/.bash_aliases
+
+ or something similar, depending on the name and location of the
+ files where the aliases are defined.
+
+ 3. Done.
+
+## Documentation
+
+Full documentation is on the manpage [`add-etc-hosts(8)`](http://github.perusio.org/add-etc-hosts).
@@ -0,0 +1,75 @@
+#!/bin/bash
+
+## add-etc-hosts --- A simple script for adding/removing a local site
+## from /etc/hosts.
+
+## Copyright (C) 2011 António P. P. Almeida <appa@perusio.net>
+
+## Author: António P. P. Almeida <appa@perusio.net>
+
+## Permission is hereby granted, free of charge, to any person obtaining a
+## copy of this software and associated documentation files (the "Software"),
+## to deal in the Software without restriction, including without limitation
+## the rights to use, copy, modify, merge, publish, distribute, sublicense,
+## and/or sell copies of the Software, and to permit persons to whom the
+## Software is furnished to do so, subject to the following conditions:
+
+## The above copyright notice and this permission notice shall be included in
+## all copies or substantial portions of the Software.
+
+## Except as contained in this notice, the name(s) of the above copyright
+## holders shall not be used in advertising or otherwise to promote the sale,
+## use or other dealings in this Software without prior written authorization.
+
+## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+## DEALINGS IN THE SOFTWARE.
+
+## The /etc/hosts file.
+ETC_HOSTS=/etc/hosts
+
+
+SCRIPTNAME=${0##*/}
+## Checking the type of action we will perform. Adding or removing.
+ACTION=$(echo $SCRIPTNAME | awk '$0 ~ /rm/ {print "REMOVE"} $0 ~ /add/ {print "ADD"} $0 !~ /(add|rm)/ {print "UNKNOWN"}')
+
+function print_usage() {
+ echo "$SCRIPTNAME <local site name>"
+}
+
+## Signal an unknown action.
+if [ "$ACTION" == "UNKNOWN" ]; then
+ echo "$SCRIPTNAME: Unknown action!"
+ print_usage
+ exit 2
+fi
+
+## Check the number of arguments.
+if [ $# -ne 1 ]; then
+ print_usage
+ exit 1
+fi
+
+## Check that /etc/hosts exists.
+[ -r $ETC_HOSTS ] || (echo "No $ETC_HOSTS file found!" && exit 3)
+
+## Perform the specified action.
+case $ACTION in
+ ADD) # Adding the new hostname to the /etc/hosts file.
+ ## Check if the hostname is already added. If so exit now.
+ grep -qE "^127\.0\.0\.1.*[[:space:]]+$1[[:space:]]*" $ETC_HOSTS && exit 0
+ sed -i "s/^\(127\.0\.0\.1.*\)$/\1 $1/" $ETC_HOSTS && echo "$1 added to $ETC_HOSTS."
+ ;;
+ REMOVE) # Removing the hostname.
+ ## Check if the given hostname exists. If not exit
+ ## now. Nothing to remove.
+ sed -n '1p' $ETC_HOSTS | grep -vqE "^127\.0\.0\.1.*[[:space:]]+$1[[:space:]]*" && exit 0
+ ## Regexes are fragile things.
+ REMOVED_HOST_LINE=$(awk -v hostname=$1 '/^127\.0\.0\.1/ {for(i=1;i<NF+1;i++) if ($i != hostname) s = i != NF ? sprintf("%s%s ", s, $i) : sprintf("%s%s", s, $i)} END {print s}' $ETC_HOSTS)
+ sed -i "s/^127\.0\.0\.1.*$/$REMOVED_HOST_LINE/" $ETC_HOSTS && echo "$1 removed from $ETC_HOSTS."
+ ;;
+esac
@@ -0,0 +1,64 @@
+add-etc-hosts, rm-etc-hosts -- add/remove an hostname to /etc/hosts
+===================================================================
+
+## SYNOPSIS
+
+`add-etc-hosts` [ <hostname> ] <br>
+`rm-etc-hosts` [ <hostname> ] <br>
+
+## DESCRIPTION
+
+This manual page documents briefly the add-etc-hosts and the
+and rm-etc-hosts commands.
+
+add-etc-hosts is a script that **enables** a local "bogus" hostname to
+be used on the loopback interface for development purposes. While
+developing sites locally it's convenient to refer to the sites through
+an handle so that they can easily be reached. The way to do that is by
+editing the `/etc/hosts` file and adding the desired hostname to the
+line relative to `127.0.0.1`. This script automates that process.
+
+Likewise, rm-etc-hosts disables a hostname by removing the hostname
+from `/etc/hosts`.
+
+If the hostname exists already in `/etc/hosts ` then trying to add it
+again has no effect.
+
+If the hostname doesn't exist in `/etc/hosts` then trying to remove it
+has no effect.
+
+## EXAMPLES
+
+Enable a site:
+
+ add-etc-hosts my.local.wp.site
+
+Disable a site:
+
+ rm-etc-hosts static.host-local
+
+
+## FILES
+
+`/etc/hosts` <br>
+The hosts file. Where local addresses are mapped to hostnames.
+
+## SEE ALSO
+
+nginx\_ensite(8), nginx\_dissite(8), a2ensite(8), a2dissite(8), nginx(1)
+
+## COPYRIGHT
+
+add-etc-hosts is Copyright (C) 2011 Antonio P. P. Almeida
+\<appa@perusio.net\> under the terms of a MIT license.
+
+## AVAILABILITY
+
+The latest version of add-etc-hosts is available from:
+[https://github.com/perusio/add-etc-hosts](https://github.com/perusio/add-etc-hosts).
+
+## AUTHOR
+
+add-etc-hosts was written by Antonio P. P. Almeida \<appa@perusio.net\>
+that also wrote the manual page.
+
@@ -0,0 +1,6 @@
+## Manuals referenced on this project.
+a2ensite(8) http://man.cx/a2ensite
+a2dissite(8) http://man.cx/a2dissite
+nginx_ensite(8) http://github.perusio.org/nginx_ensite
+nginx_dissite(8) http://github.perusio.org/nginx_ensite
+nginx(1) http://man.cx/nginx

0 comments on commit 4dc9b81

Please sign in to comment.