Last active
January 8, 2016 10:49
-
-
Save renatofmachado/d37212677a45b88aaf33 to your computer and use it in GitHub Desktop.
Hosts script to help edit the host names.
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/bash | |
# Lists all of the available host names. | |
function list(){ | |
ips=$(cat "$file" | grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | tr -s "\t " "\t") | |
numberOfFiles=$(echo -e "$ips" | wc -l | tr -d " ") | |
echo -e "\nThere are $numberOfFiles hosts registered:\n\n$ips" | |
} | |
# Adds an host to the list | |
# @param ip The IP address of the host. | |
# @param name The host name. | |
function add(){ | |
if [ "$1" == "" ] | |
then | |
echo -e "Error: The add method requires at least one argument." | |
exit 1; | |
fi | |
ip=$localhost | |
name=$1 | |
if [ "$2" != "" ] | |
then | |
ip=$1 | |
name=$2 | |
else | |
name=$1 | |
fi | |
echo -e "$ip\t$name" >> "$file" | |
echo -e "The \"$name\" host name was successfully added to your hosts file." | |
} | |
# Removes an host from the list | |
# @param name The host name to be removed. | |
function remove(){ | |
if [ "$1" == "" ] | |
then | |
echo -e "The add method requires one argument." | |
exit 1; | |
fi | |
result=$(cat "$file" | grep -iw "$1" | wc -l | tr -d " ") | |
if [ "$result" != "0" ] | |
then | |
content=$(cat "$file" | grep -viw "$1") | |
echo "$content" > "$file" | |
echo -e "\nThe \"$1\" host was deleted with success." | |
else | |
echo -e "\nError: No host names under \"$1\" were found." | |
fi | |
} | |
# Edits the host name. | |
# @param name The name of the host to be edited. | |
# @param -i The new IP address. | |
# @param -n The new host name. | |
function edit(){ | |
if [ "$1" == "" ] && [ "$2" == "" ] && [ "$3" == "" ] | |
then | |
echo -e "Error: The edit method requires at least 3 arguments." | |
exit 1; | |
fi | |
oldName="$1" | |
oldIp=$(cat "$file" | grep "$oldName" | tr -s "\t " " " | cut -d " " -f1) | |
if [ "$oldIp" == "" ] | |
then | |
echo -e "\nError: The host name \"$oldName\" does not exist." | |
exit 1; | |
fi | |
if [ "$2" == "-i" ] | |
then | |
newIp="$3" | |
else | |
if [ "$2" == "-n" ] | |
then | |
newName="$3" | |
else | |
echo -e "Error: Invalid \"$2\" argument." | |
exit 2; | |
fi | |
fi | |
if [ "$4" != "" ] | |
then | |
if [ "$5" == "" ] | |
then | |
echo -e "Error: The edit method is missing one argument." | |
exit 2; | |
fi | |
if [ "$4" == "-i" ] | |
then | |
newIp="$5" | |
else | |
if [ "$4" == "-n" ] | |
then | |
newName="$5" | |
else | |
echo -e "Error: Invalid \"$4\" argument." | |
exit 2; | |
fi | |
fi | |
fi | |
if [ "$newIp" == "" ] | |
then | |
newIp=$oldIp | |
fi | |
if [ "$newName" == "" ] | |
then | |
newName=$oldName | |
fi | |
remove $oldName | |
add $newIp $newName | |
} | |
# Provides information about the script, and how to use it. | |
function help() { | |
echo -e " | |
Hosts is a custom command that edits the etc/hosts file.\n | |
Usage: | |
hosts [command] [arguments..] (optional)\n | |
List of available commands:\n | |
add (ip) [name] - Maps an IP address to a host name. | |
Arguments: | |
name: The host name. | |
Optional: | |
ip: The IP address of the host. If not given, the script will assume the usage of $localhost. | |
remove [name] - Removes an host name. | |
Arguments: | |
name: The host name. | |
edit [name] [option] [arguments] | |
Arguments: | |
name: The host name to be edited. | |
Options: | |
-i: Requires an IP address. | |
-n: Requires a host name. | |
list - Lists all of the registered hosts. | |
" | |
} | |
if [ "$(uname)" == "Darwin" ] || [ "$(echo $(uname -s) | cut -c 1-5)" == "Linux" ] | |
then | |
# We're using OSX/Linux. | |
file="/etc/hosts" | |
else | |
# We're using Windows OS. | |
file="/c/Windows/System32/drivers/etc/hosts" | |
fi | |
localhost="127.0.0.1" | |
if [ "$1" != "" ] | |
then | |
command=$1 | |
shift | |
case $command in | |
add) add $*;; | |
-a) add $*;; | |
list) list;; | |
-l) list;; | |
remove) remove "$1";; | |
-r) remove "$1";; | |
edit) edit $*;; | |
help) help;; | |
-h) help;; | |
esac | |
else | |
echo -e "\nError: At least one argument is necessary. Use \"hosts help\" to see all of the available commands." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Go back to India you pleb