1

I want to test connectivity to DBs via a shell script that will SSH on to a number of boxes and then test the mysql connection. I have SSH keys working so I am not explicitly specifying user@server in the ssh command. The script is not working as i would expect and prompts me for a password but errors and uses the password as a DB name. Can some one please advise?

#!/bin/bash

SERVER_LIST=(172.10.1.1 172.10.1.2 172.10.1.3)
CONNECTION=172.0.0.10
USER="username"
PASS="password"

echo "Testing connectivity... "
for SERVER in ${SERVER_LIST[@]}
do
    echo "SSHing to $SERVER"
    ssh $SERVER "mysql -h $CONNECTION -u $USER -p$PASS"
done
echo "Finished."

After some reading around, should i have a expects command or similar on the password prompt? I have experimented with this to no avail...

3
  • 1
    Did your password have specials characters ? Can you show us what you get when you execute script ? Commented Jul 16, 2014 at 13:47
  • Sure thing Enter password: password ERROR 1049 (42000): Unknown database 'password' Commented Jul 16, 2014 at 15:18
  • First of all, try your mysql command outside of your bash script and outside of ssh. Then test it with the ssh command and then with the bash script. If you don't use root user, I think you have to specify a db name. Commented Jul 16, 2014 at 20:17

2 Answers 2

1

You must export you public key to authorized_keys in your .ssh folder in the server

  1. Generate your keys

    ssh-keygen -t rsa

  2. Create .ssh on the server if it doesn't exists ssh b@B mkdir -p .ssh

  3. Send them to the server

    cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'

  4. Test your login

    ssh b@B

If anything goes wrong use ssh -v b@B to figure it out

Sign up to request clarification or add additional context in comments.

1 Comment

sorry, i should have mentioned that I have ssh keys to establish the SSH connection, the ssh connection is not the concern as far as I can see as I am able to get onto the boxes, it is the mysql connection that is the issue
1
Enter password: password ERROR 1049 (42000): Unknown database 'password'

From the error message, the mysql client erroneously understand password as the DB name. And if it asks for the password, that means it receive the -p option without a password. So, all behave like if the client has received the command mysql .... -p password (notice the space after p).

My guess is there is somehow a space before the password somewhere. Maybe in the following line:

PASS="password"

If you copied-pasted that password, maybe there is an invisible character before (zero-width space?). Try to remove, then hand-write that line.

In addition, try proper quoting (not that proper: this won't work if any of your variables holds a '). At the very least, this might help to narrow the problem:

ssh "$SERVER" "mysql -h '$CONNECTION' -u '$USER' -p'$PASS'"

2 Comments

I'm agree with you that's what I asked for special characters in password.
@kranteg Yes, I've up-voted your comment about that yesterday. I would point out that some "special characters" might indeed be "unintentional" and "invisible". I've already have that kind of issue with users having copied-pasted their login/password from their webmail. For formating purpose, some invisible characters where added and sneaked in with the original text as the user copied it from his browser. This is highly speculative though. And without further feedback of the OP, I think we're stuck from now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.