On Thu, 16 Jun 2011, Paul Fierro wrote:

> Trying to run this but it isn't working - I'm suspecting it's the 
> quotes:
>
> $ ssh machine1 "ssh machine2 "mysql -e "show databases"""
>
> I tried various combinations of single and double quotes as well as 
> backslashes. Can anyone help?


I have a machine behind a firewall (I'll call it box2) that I can only 
access by first logging into another machine (which I'll call box1). 
Let's say that on box1, I am user1 and on box2, I am user2.  I just tested 
this one-liner and it worked:

ssh -f -L 25922:box2:22 user1 at box1 sleep 1 ; ssh -p 25922 user2 at localhost

It could be written as a two-line script.  It first prompts for the box1 
password, then for the box2 password.  When it's done it looks like I'm 
logged in from the machine I'm touching, but box2 says I'm logged in from 
box1.

The "sleep 1" is used instead of "-N" so that the ssh port forward is 
killed soon after I terminate the ssh session with box2 (a.k.a., localhost 
port 25922).  That way, when I run it again, it won't give me an error 
because the port is already in use.

So I think the show database thing could be done like this (assuming the 
username is the same on every box):

ssh -f -L 25922:machine2:22 machine1 sleep 1 ; ssh -p 25922 localhost 'mysql -e "show databases"'

Or on two lines in a script:

ssh -f -L 25922:machine2:22 machine1 sleep 1
ssh -p 25922 localhost 'mysql -e "show databases"'

It might be a little easier to read the script if you use the port 
forwarding scheme.

Mike