Skip to content

Instantly share code, notes, and snippets.

@muffycompo
Forked from Trle94/mkuser.sh
Created April 5, 2018 10:48
Show Gist options
  • Save muffycompo/11c31b0288e8add1840cd2502be2d007 to your computer and use it in GitHub Desktop.
Save muffycompo/11c31b0288e8add1840cd2502be2d007 to your computer and use it in GitHub Desktop.
#!/usr/bin/bash
#title :mkuser.sh
#description :This script will make random password for root, new user, new user as sudo, clear history
#author :Trle94 @ Castlegem SRL
#date :03.04.2018
#version :0.1
#usage :./mkuser.sh
#--------------------------------------------------------------------------------
[ $EUID -eq 0 ] || { echo "This script needs to be ran with SUDO!"; exit 1; }
#---------------COLOR PALLETE--------------
RED='\033[1;31m'
NC='\033[0m' # No Color
GREEN='\033[1;32m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
#------------------------------------------
#---------------QUESION FUNCTION--------------
AQ() {
local prompt default reply
while true; do
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
echo -e "$1 [$prompt] "
read reply </dev/tty
# Default answer check
if [ -z "$reply" ]; then
reply=$default
fi
# Check if the reply is valid
case "$reply" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
#------------------------------------------
#------------------------------------------
#--------Main function goes here-----------
#------------------------------------------
echo -e "Hi, please enter password length[INT]: \c "
read -r LENG_PASSWD
if [[ -z "${LENG_PASSWD//[0-9]}" ]] && [[ -n "$LENG_PASSWD" ]];then #Check if password length has numbers only and not empty
# declare NEW_PASSWD variable/ generating password
NEW_PASSWD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9!@#$' | fold -w $LENG_PASSWD | head -n 1)
#Changing user password!! echo new password if success or exit if failed!
echo -e "$NEW_PASSWD\n$NEW_PASSWD" | passwd root &>/dev/null && { echo -e "Your "${GREEN}"root"${NC}" password has been updated, new password is:" ${CYAN}$NEW_PASSWD${NC}; } \
|| { echo -e ${RED}"Password change failed!"${NC} ; exit 1; }
# Question creating new user
if AQ "Would you like to create new user?" Y; then
echo -e "Enter new username: \c "
read -r NewUser
if [[ -z "${NewUser//[a-z]}" ]] && [[ -n "$NewUser" ]];then #Check if user has letters only and not empty
useradd $NewUser #Adding new user
echo -e "$NEW_PASSWD\n$NEW_PASSWD" | passwd ${NewUser} &>/dev/null && { echo -e "Your "${GREEN}${NewUser}${NC}" password has been updated, new password is:" ${CYAN}$NEW_PASSWD${NC}; } \
|| { echo -e ${RED}"Password change failed!"${NC} ; exit 1; } #Setting password to created user same as root one
if AQ "Would you like to add new user to sudoers?" Y; then #Adding new user to SUDOERS
echo -e ${NewUser}" ALL=(ALL) ALL" >> /etc/sudoers
fi
else # If check failed start from begin!!
echo -e ${CYAN}"Please, use letters only!"${NC}
exec "$0" "$@"
fi
fi
else # If check failed start from begin!!
echo -e ${CYAN}"Please, enter integer!"${NC}
exec "$0" "$@"
fi
if AQ "Would you like to clean history and exit?" Y; then # Clearing history and leaving!!
cat /dev/null > ~/.bash_history && history -c && history -w && rm $0 && exit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment