1

I am having some trouble with a script of mine that is supposed to SSHs into my server, run a mysql query, and return a specific value.

My problem is that I am getting an Access Denied error when I use command substitution [ $() and `` ] - but without command substitution I don't get the result saved to a variable.

I have full privileges, and the password is correct.

  ssh [email protected]  << EOFMARK
    example=$(mysql -h localhost -u root -p$MYSQL_PASSWORD myDatabase -e "SELECT now();")  
    echo "Variable is $example"
  EOFMARK

echo "My goal is to read the variable here: $example"
1
  • Please check if you are using password that has privileges on localhost..you can check by show grants for root@'localhost' and your password in script should be same as password by this command. Commented Mar 7, 2014 at 7:29

2 Answers 2

1

You might want to

  • immediately execute the command and
  • give it the query it is supposed to execute.

This way:

example=$(ssh [email protected] mysql -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e '"SELECT now();"')
echo "My goal is to read the variable here: $example"

would be the way to go.

If you really need it at both places, you either might want to have a script on the server, or you could try

example=$(ssh [email protected] 'example=$(mysql -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e "SELECT now();"); echo "Variable is $example")
echo "My goal is to read the variable here: $example"

In order to stick on the heredoc, you could as well try

example=$(ssh [email protected]  << EOFMARK
    example=$(mysql -h localhost -u root -p$MYSQL_PASSWORD myDatabase -e "SELECT now();")  
    echo "Variable is $example"
EOFMARK)

echo "My goal is to read the variable here: $example"

in order to have the variable on the client side as well.


Due to whatever, I can't get my head around the heredoc example.

But the others should look like

example=$(ssh [email protected] 'mysql -NB -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e "SELECT now();"')
echo "My goal is to read the variable here: $example"

which gives exactly the output into the local variable, or

example=$(ssh [email protected] 'example=$(mysql -NB -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e "SELECT now();"); echo "Variable is $example")
echo "My goal is to read the variable here: $example"

but you should be aware that here, the local output looks like (e. g.)

My goal is to read the variable here: Variable is 2014-03-07 20:42:23

as the sub string Variable is gets output as well.

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

3 Comments

I am adapting my script to this answer but it apparently has a few rough edges. For a bash noob I'm pretty stuck in escaping everything. Could you address this issue?
@JHAWN Which of the 3?
The last one would be the best, though all three work. (Ignoring the escaping issues)
1

You should escape the substitutions that has to be done at remote host:

Try:

 ssh [email protected]  << EOFMARK
    example=\$(mysql -h localhost -u root -p$MYSQL_PASSWORD myDatabase -e "SELECT now();")  
    echo "Variable is \$example"
  EOFMARK

You can follow @glglgl advise, and capture standard output of your ssh command into a local variable. But I would use a local mysql client and I would grant privileges to access database. If you have a firewall that prevents you from remote accesing 3306, I would ssh forward port.

example=$(mysql -h SERVER.com -u root -p myDatabase -e "SELECT now();") 

That's the right approach. No ssh, and interactive password, no accesible via ps -ef| grep mysql

1 Comment

This works perfectly from within the scope of the SSH heredoc, however I need access to the variable from outside of the heredoc as well. Could you advise?

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.