From 60ecccc5591457d439c2277c29bcb40da12cec9d Mon Sep 17 00:00:00 2001 From: Jus de Patate Date: Mon, 4 Feb 2019 10:28:39 +0100 Subject: [PATCH] Upload hosts function --- plugins/available/hosts.plugin.bash | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 plugins/available/hosts.plugin.bash diff --git a/plugins/available/hosts.plugin.bash b/plugins/available/hosts.plugin.bash new file mode 100644 index 00000000..885748b6 --- /dev/null +++ b/plugins/available/hosts.plugin.bash @@ -0,0 +1,23 @@ +# Requested by philipjohn here : https://github.com/Bash-it/bash-it/issues/1264 + +function hosts() { + if [ ! "$(whoami)" = "root" ]; then echo "Error: Run this function as root (using sudo)" && exit 1; fi + if [ "$1" = "add" ]; then + if [[ "$2" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + shift + echo "$1 $2" >> /etc/hosts + echo "Added $1 $2 to hosts file" + else + echo "Error : Provided IP is invalid" + fi + elif [ "$1" = "list" ]; then + cat hosts + elif [ "$1" = "del" ] || [ "$1" = "remove" ]; then + shift + grep -v "$1" /etc/hosts > newhosts + mv newhosts /etc/hosts + echo "Removed $1 from hosts file" + else + echo "Missing argument (del, list, add)" + fi +}