Upload hosts function

pull/1321/head
Jus de Patate 2019-02-04 10:28:39 +01:00 committed by GitHub
parent 71ee7083cf
commit 60ecccc559
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 0 deletions

View File

@ -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
}