everything
This commit is contained in:
commit
6f8c29ffb8
238
autodropuser.sh
Normal file
238
autodropuser.sh
Normal file
@ -0,0 +1,238 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Exit codes:
|
||||
# 1 :
|
||||
# 2 :
|
||||
# 3 :
|
||||
# 4 :
|
||||
|
||||
|
||||
########################################################################
|
||||
# INIT
|
||||
########################################################################
|
||||
#CONFIGFILE="$(dirname $(readlink -f $0))/$(basename $(readlink -f $0) .sh).config"
|
||||
########################################################################
|
||||
#/INIT
|
||||
########################################################################
|
||||
|
||||
|
||||
|
||||
########################################################################
|
||||
#
|
||||
# CONSTANTS
|
||||
#
|
||||
########################################################################
|
||||
|
||||
# colors
|
||||
BOLD="\e[1m"
|
||||
GREEN="\e[32m"
|
||||
LIGHTGREEN="${BOLD}${GREEN}"
|
||||
RED="\033[1;31m"
|
||||
LIGHTRED="\033[1;31m"
|
||||
BLUE="\e[34m"
|
||||
LIGHTBLUE="${BOLD}${BLUE}"
|
||||
YELLOW="\e[33m"
|
||||
LIGHTYELLOW="${BOLD}${YELLOW}"
|
||||
WHITE="\033[0;37m"
|
||||
RESET="\033[0;00m"
|
||||
|
||||
NOW="$(date +%Y%m%d%H%M%S)"
|
||||
|
||||
DEBUG=0
|
||||
DEBUG=1
|
||||
|
||||
########################################################################
|
||||
#
|
||||
# / CONSTANTS
|
||||
#
|
||||
########################################################################
|
||||
|
||||
|
||||
|
||||
|
||||
########################################################################
|
||||
#
|
||||
# VARIABLES
|
||||
#
|
||||
########################################################################
|
||||
|
||||
|
||||
SCRIPTLOG="$(dirname `readlink -f $0`)/logs/$(basename $0 .sh)_script_${NOW}.log"
|
||||
SCRIPTLOGERR="$(dirname `readlink -f $0`)/logs/$(basename $0 .sh)_script_${NOW}.err"
|
||||
|
||||
|
||||
THEUSERNAME=""
|
||||
THEHOSTNAME=""
|
||||
THEPORT=""
|
||||
THEADMINUSER=""
|
||||
|
||||
########################################################################
|
||||
#
|
||||
# / VARIABLES
|
||||
#
|
||||
########################################################################
|
||||
|
||||
|
||||
|
||||
########################################################################
|
||||
#
|
||||
# FUNCTIONS
|
||||
#
|
||||
########################################################################
|
||||
|
||||
|
||||
usage()
|
||||
{
|
||||
printf "%s${LIGHTRED}USAGE:${RESET}
|
||||
$0 -u USERNAME -h HOSTNAME -p PORT -a ADMINUSER [-D] [-F]
|
||||
|
||||
-u USERNAME ${LIGHTYELLOW}USERNAME${RESET} to be dropped
|
||||
-h HOSTNAME ${LIGHTYELLOW}HOSTNAME${RESET} of the database
|
||||
-p PORT ${LIGHTYELLOW}PORT${RESET} of the database
|
||||
-a ADMINUSER ${LIGHTYELLOW}ADMINUSER${RESET} wit priviledges to perform the action
|
||||
-D DEBUG mode
|
||||
-F is the force with you?
|
||||
"
|
||||
# VERY INITIAL CHECKS
|
||||
}
|
||||
|
||||
printmsg()
|
||||
{
|
||||
echo -e "$*"
|
||||
}
|
||||
|
||||
output_log()
|
||||
{
|
||||
if [[ "${QUIETOUTPUT}" == true ]]; then
|
||||
printmsg "$*" >> ${OUTPUTFILE}
|
||||
else
|
||||
printmsg "$*" | tee -a "${OUTPUTFILE}"
|
||||
fi
|
||||
}
|
||||
|
||||
abort_message()
|
||||
{
|
||||
printmsg "${LIGHTRED}ERROR${RESET}: $*"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# debug_me uses variable ${DEBUGME}
|
||||
debug_me()
|
||||
{
|
||||
if [[ "${DEBUGME}" && ${DEBUGME} -eq 0 ]] ; then
|
||||
echo -e "${LIGHTBLUE}DEBUG: ${RESET}$*"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
drop_selected_user()
|
||||
{
|
||||
#dropuser -W -p $1 -h $2 -U $3 -e $4
|
||||
echo
|
||||
if [[ ${MAYTHEFORCEBEWITHYOU} != "true" ]] ; then
|
||||
echo -e "Do you want to drop the user ${THEUSERNAME}?? (y/N)"
|
||||
read -t10 YN
|
||||
if [[ "${YN,,}" != "y" ]] ; then
|
||||
# drop_vm
|
||||
# RES=$?
|
||||
# else
|
||||
echo -e "${LIGHTRED}${THEUSERNAME}${RESET} NOT droped, exitting"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
fi
|
||||
${DROPUSERBIN} -W -p ${THEPORT} -h ${THEHOSTNAME} -U ${THEADMINUSER} -e ${THEUSERNAME}
|
||||
}
|
||||
|
||||
########################################################################
|
||||
#
|
||||
# / FUNCTIONS
|
||||
#
|
||||
########################################################################
|
||||
|
||||
########################################################################
|
||||
#
|
||||
# MAIN
|
||||
#
|
||||
########################################################################
|
||||
|
||||
|
||||
[[ ! -d $(dirname ${SCRIPTLOG}) ]] && mkdir -p $(dirname ${SCRIPTLOG})
|
||||
[[ ! -d $(dirname ${SCRIPTLOGERR}) ]] && mkdir -p $(dirname ${SCRIPTLOGERR})
|
||||
|
||||
# DETECTING if the script is run by cron
|
||||
if [[ "$(tty)" = "not a tty" ]] ; then
|
||||
set -x
|
||||
exec > ${SCRIPTLOG}
|
||||
exec 2> ${SCRIPTLOGERR}
|
||||
fi
|
||||
|
||||
if [[ ${DEBUG} -eq 0 ]] ; then
|
||||
echo -e "${BLUE}DEBUGMODE${RESET} is on"
|
||||
echo -e "\t SCRIPTLOG will be ${SCRIPTLOG}"
|
||||
echo -e "\t SCRIPTLOGERR will be ${SCRIPTLOGERR}"
|
||||
set -x
|
||||
#exec 2> ${SCRIPTLOGERR}
|
||||
fi
|
||||
|
||||
if [[ ! "$*" ]] ; then
|
||||
usage
|
||||
exit 0
|
||||
fi
|
||||
|
||||
#port="$1"
|
||||
#host="$2"
|
||||
#adminUser="$3"
|
||||
#userToDelete="$4"
|
||||
|
||||
while getopts "u:h:p:a:DF" arg; do
|
||||
case $arg in
|
||||
u)
|
||||
export THEUSERNAME=${OPTARG}
|
||||
;;
|
||||
h)
|
||||
export THEHOSTNAME=${OPTARG}
|
||||
;;
|
||||
p)
|
||||
export THEPORT=${OPTARG}
|
||||
;;
|
||||
a)
|
||||
export THEADMINUSER=${OPTARG}
|
||||
;;
|
||||
F)
|
||||
MAYTHEFORCEBEWITHYOU=true
|
||||
echo -e "${LIGHTBLUE}The force is strong in you?${RESET}"
|
||||
;;
|
||||
D)
|
||||
DEBUG=0
|
||||
;;
|
||||
*)
|
||||
usage
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
[[ ${DEBUG} -eq 0 ]] && DEBUGME="bash -x"
|
||||
|
||||
if [[ ! "${THEUSERNAME}" =~ ^[a-zA-Z].{1,}$ ]] ; then
|
||||
abort_message "The USERNAME must begin with a letter an have at least 3 characters"
|
||||
fi
|
||||
|
||||
DROPUSERBIN=$(which dropuser)
|
||||
|
||||
if [[ ! "${DROPUSERBIN}" ]] ; then
|
||||
abort_message "drop user binary not found on \${PATH}, exiting"
|
||||
fi
|
||||
|
||||
drop_selected_user
|
||||
|
||||
|
||||
exit ${EXITCODE}
|
||||
|
||||
########################################################################
|
||||
#
|
||||
# / MAIN
|
||||
#
|
||||
########################################################################
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user