User authentication from batch file - mysql

How can I authenticate a valid user already registered on mysql automatically ? (Can be a batchscript, or vbs or anything that run automatically from windows) and if the user exist and the password is correct, call another batch ?

You could login with the command line client like so
mysql -uroot -ppassword -e"exit" && SET validlogin=true || SET validlogin=false
IF %validlogin% == true (ECHO run batch here) ELSE (ECHO don't run batch)
Some dos guru could probably do better

Related

Setting envs or globals in bash script [NOT A duplicate]

I'm looking for a way (preferably cross-platform compatible) to set something globally accessible from a bash script.
My company is using a bash script to request access credentials to a mysql database. This returns username, password and db domain that I end up having to copy paste in my terminal to run and connect to our mysql db.
I thought i'd amend the script to set environment variables and make use of these in an alias with the credentials set in my bashrc but turns out you can't set environment variables in a bash script.
So i tried to set the mysql alias with the username password and domain pre-filled in that same script but same issue. Can't set an alias in a bash script.
I essentially want to be able to run the script that gives me the credentials and then not have to do manual copy pasting all over the place.
What I tried was (if it give more context):
#!/bin/bash
# Script gets the credentials
# Script now has username, password, endpoint variables
export MYSQL_USER=$username
export MYSQL_PASSWORD=$password
export MYSQL_ENDPOINT=$endpoint
# Script finishes
and in my bashrc:
alias mysqlenv="mysql -h $MYSQL_ENDPOINT -u $MYSQL_USER -p'$MYSQL_PASSWORD'"
I appreciate this is not working and that might not be the best solution so i'm open to other options.
PS: Forgot to mention the credentials expire every 24H which is why i want to smoothen the process
PS2: I can't source the script that gives me the credentials because it's not just exporting environment variables, it's taking params from the cli and getting me to log in to my company system on my browser, etc.
PS3: I know putting password for mysql on the command line is bad practice but this is a non-issue as that password is being printed there in the first place by the script that give me the credential (written by someone else in the company)
Since you can already parse the credentials, I'd use your awk code to output shell commands:
getMysqlCredentials() {
credential_script.sh | awk '
{parse the output}
END {
printf "local MYSQL_USER=\"%s\"\n", username
printf "local MYSQL_PASSWORD=\"%s\"\n", password
printf "local MYSQL_ENDPOINT=\"%s\"\n", endpoint
}
'
}
then, I'd have a wrapper function around mysql where you invoke that function and source the output
mysql() {
source <(getMysqlCredentials)
command mysql -h "$MYSQL_ENDPOINT" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$#"
}

Can't login to unix mysql user

I'm trying to login to my mysql user created by the MySQL installation in bash shell.
So the command I'm trying to do is:
sudo -i -u mysql
But, it does nothing (nothing printed out in the console, not connected to any user whatsoever, etc...).
EDIT: The return value of the command is 1 though.
Here is the mysql user line in the /etc/passwd file:
mysql:x:89:89::/var/lib/mysql:/bin/false
I'm running on ArchLinux 64bit and the user I'm trying to connect to is local (no connection to a remote server whatsoever).
I'm trying to understand why I can login with any other user (like postgres, root, ...) but not mysql.
Hope you can help!
This is intended behaviour. When you compare the /etc/passwd line with other lines you'll notice that the user mysql has /bin/false setup as the shell while others have usually /bin/sh or sth. similar.
When you sudo to mysql you actually get a login for about a millisecond or so and then his "shell" gets executed. /bin/false immediately returns with rc=1 (that's the only purpose of the false command).
This is some kind of "trick" to prevent users from logging in as user mysql although the account is otherwise fully operational.
The user mysql may even have a valid password and be enabled but due to his odd shell setting you cannot login as him. Sometimes these technical users have /usr/bin/passwd set as their shell. Then you can only sudo to that user and change his password, nothing else.

mySQL Script Error

I've been trying to get an OpenVPN server running Debian-7-Wheezy to authenticate via mySQL all day. I have mySQL hosted on another server running WHM and cPanel. I added the OpenVPN server's IP to the whitelist for database connections, checked all my credentials and double checked the config file. When I run: /etc/openvpn/script/test_connect_db.sh test 1234 I recieve this error:
/etc/openvpn/script/test_connect_db.sh: line 6: mysql: command not found
authentication failed.
The file located at: /etc/openvpn/script/test_connect_db.sh looks exactly like this:
#!/bin/bash
. /etc/openvpn/script/config.sh
##Test Authentication
username=$1
password=$2
user_id=$(mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -sN -e "select user_id from user where user_id = '$username' AND user_pass = '$password' AND user_enable=1 AND user_start_date != user_end_date AND TO_DAYS(now()) >= TO_DAYS(user_start_date) AND (TO_DAYS(now()) <= TO_DAYS(user_end_date) OR user_end_date='0000-00-00')")
##Check user
[ "$user_id" != '' ] && [ "$user_id" = "$username" ] && echo "user : $username" && echo 'authentication ok.' && exit 0 || echo 'authentication failed.'; exit 1
Any idea what's wrong?
It sounds like you aren't event getting far enough to attempt a mysql connection to begin with.
The error you specified indicated it can't even find the mysql client.
Can you successfully just run "mysql" from the command line outside the script?
If so that would indicate your $PATH variable isn't set properly in scope of the script.
You didn't say what config.sh is doing. Maybe that's over writing the PATH variable?
Either way if you can run mysql from the command line then run
which mysql
That should give you the fully qualified path to the client binary and you could use that in your script.

Expect scripting: remote database backup automation

I'm looking for a kind of remote database backup automation.
Then, I came across a scripting language which commonly used for administrative tasks, "Expect scripting" and I believe it could serve my purpose very well.
what I'd like to do is I want to perform login to a remote server using the following bash script from my local linux box. (supposed everything has been set properly, SSH authentication via generated key pair, so no password is required)
For the most important part, I'd like to send a mysqldump command to perform backup for my database on that server.
#!/usr/bin/expect
set login "root"
set addr "192.168.1.1"
spawn ssh $login#$addr
expect "#"
send "cd /tmp\r"
expect "#"
send "mysqldump -u root -ppassword my_database > my_database.sql\r"
expect "#"
send "exit\r"
The only problem I found here was after the line send "mysqldump -u root....... ".
It was never waiting until the process to finish, but immediately exit the shell with 'send "exit\r"' command line.
what do I do to make it waits until mysqldump command finish and log off the SSH properly?
I don't know the answer to your question: add exp_internal 1 to the top of the program to see what's going on.
However, since you have ssh keys set up, you don't really need expect at all:
ssh $login#$addr 'cd /tmp && mysqldump -u root -ppassword my_database > my_database.sql'

How can I check the mysql connection for a user/password using batch/shell script

How can I check the mysql connection for a user/password using batch/shell script?
I've user name and password, and I need to authenticate whether they are correct or not, but how?
I tried this way:
I've created a batch file "run.bat" with "mysql -u User--password=UserPassword < commands.sql"
"commands.sql" file contains "\q"
When I run the file "run.bat" the output is nothing when User/Password are correct and "ERROR 1045 (28000): Access denied for user ... " when User/Password are incorrect. Now can we capture this output, and decide whether the connection is successful or not, if yes how?
Regards
I found a solution as below:
#echo OFF
echo \q | mysql -u User --password=UserPassword 2>nul
if "%ERRORLEVEL%" == "0" (
echo CONNECTION SUCCESSFUL
) else (
echo CONNECTION FAILED
)
You can check the return status of mysql. It is stored in the ERRORLEVEL enviroment variable:
mysql -u User--password=UserPassword < commands.sql
if "%ERRORLEVEL%" EQU "0" (
echo OK
) else (
echo FAIL
)
If you are lucky, mysql.exe even returns a specific status for "logon failed" that you can react on. Most applications return 0 on success and something != 0 on failure. Use echo %ERRORLEVEL% right after a command to find out the current value.
A more advanced approach would be to capture and evaluate the STDERR stream of the application. This, however, would be material for a different question.
you could use a ".my.cnf" file
I do this, although id strongly recommend against using your mysql root login
[root#daaorc900c ~]# cat ./.my.cnf
[client]
user=monitoruser
password=whatismonitor
[root#daaorc900c ~]#
Looks like you might be one windows so here is the doc for the "options files" in widnows
http://dev.mysql.com/doc/refman/5.1/en/option-files.html