Skip to content

Instantly share code, notes, and snippets.

@hadisinaee
Last active March 24, 2018 12:20
Show Gist options
  • Save hadisinaee/04e500df7c4a61650ac6507664b4a421 to your computer and use it in GitHub Desktop.
Save hadisinaee/04e500df7c4a61650ac6507664b4a421 to your computer and use it in GitHub Desktop.
Mongodb login creator with authentication. It is used for creating mongodb login scripts so that you easily can login into mongodb.
# declaring some colors for pretty output
declare -A colors=( ["g"]="\e[32m" ["r"]="\e[31m" ["y"]="\e[33m" ["b"]="\e[34m" ["m"]="\e[35m" ["c"]="\e[36m" ["default"]="\e[39m" )
declare -A sets=( ["b"]="\e[1m" ["d"]="\e[2m" ["u"]="\e[4m" ["bl"]="\e[5m")
declare -A resets=( ["b"]="\e[21m" ["d"]="\e[22m" ["u"]="\e[24m" ["bl"]="\e[25m")
# declare colorful echo
function echoc {
STRING_PRIFIX=''
if [ -n "$2" ]
then
STRING_PRIFIX=$STRING_PRIFIX${colors[$2]}
fi
if [ -n "$3" ]
then
STRING_PRIFIX=$STRING_PRIFIX${sets[$3]}
STRING_POSTFIX=${resets[$3]}
fi
echo -e "$STRING_PRIFIX"$1"${colors[default]}$STRING_POSTFIX"
}
function main {
INFO=$1
SUCCESS=$2
ERROR=$3
PORT_NUMBER=27017
echoc "[optional]: Please enter port number(default 27017) [ENTER]: " "$INFO" "b"
read TEMP_INPUT
PORT_NUMBER=${TEMP_INPUT:-$PORT_NUMBER}
DB_HOSTNAME="localhost"
echoc "[optional]: Please enter host address (default localhost) [ENTER]: " "$INFO" "b"
read TEMP_INPUT
DB_HOSTNAME=${TEMP_INPUT:-$DB_HOSTNAME}
DB_NAME="admin"
echoc "[optional]: Please enter authentication database name(default admin) [ENTER]: " "$INFO" "b"
read TEMP_INPUT
DB_NAME=${TEMP_INPUT:-$DB_NAME}
echoc "[required]: Please enter username for given database [ENTER]: " "$INFO" "b"
read DB_UESRNAME
if [ -z $DB_UESRNAME ]
then
echoc "empty username provided. Please enter a valid username." "$ERROR" "b"
exit
fi
echoc "[required]: Please enter password [ENTER]:" "$INFO" "b"
read DB_PASSWORD
if [ -z $DB_PASSWORD ]
then
echoc "empty password provided. Please enter a valid password." "$ERROR" "b"
exit
fi
DB_LOGIN_COMMAND="mongo --port $PORT_NUMBER --host $DB_HOSTNAME --username $DB_UESRNAME --password $DB_PASSWORD --authenticationDatabase $DB_NAME"
LOGIN_COMMAND_FILE=$DB_UESRNAME"_"$DB_HOSTNAME".sh"
echoc "[DONE]: Command created successfully" "$SUCCESS" "b"
echoc "\t$DB_LOGIN_COMMAND" "$SUCCESS"
printf "\n"
echoc " \tmake the command executable by:" "$SUCCESS"
echoc "\t chmod +x $LOGIN_COMMAND_FILE" "$SUCCESS" "bl"
echo $DB_LOGIN_COMMAND > $LOGIN_COMMAND_FILE
}
# $1: info $2: success $3: error
main "m" "g" "r"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment