Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to set a variable to the output of a ssh command in zsh. The ssh command itself connects to a first server, then ssh again from this server to a second server where I want to execute my command.

Here is the command (works fine when typed in the shell) :

ssh -t host1 "ssh -t host2 "uname -a""

I have an ssh key setup for host1, so the connection doesn't ask for a password. I don't have one for the subsequent connection between host1 and host2, so I have to type the password.

Now, I would like to save the content of uname in a variable, to be used in my zsh script. I naively tried :

a=$(ssh -t host1 "ssh -t host2 "uname -a"")

but it seems to hang forever, somehow the interactive password login prompt is not redirected to me. If I try a echo $a afterwards I got a user@host2's password:' to confirm it

My guess is that something went wrong in the way zsh handles quotes and double quotes, but I could not figure out what. I've tried many variations, without success.

share|improve this question
up vote 0 down vote accepted

It's definitely not related to quoting (which should probably be ssh -t host1 "ssh -t host2 \"uname -a\""). Quotes only apply to the command-line arguments, not to the command input/output. Only $() can affect the output.

The problem might be simply how ssh handles tty input/output. It seems that ssh (here ssh -t host1) does not use the client-side terminal device even if it has one available – it writes to stdout instead, which $() will capture.


The best solution would be to set up key authentication between local and host2, then add your keys to ssh-agent, and use the "Agent forwarding" option: ssh -A host1 ssh host2 uname -a. This way you don't need to copy your private key anywhere else.

share|improve this answer
    
Thanks. I cannot manually set key authentication between local and host2 (actually, the whole point of the script I'm trying to write was to automate this). I didn't know about the -A option either, and I finally found an alternative solution : executing my script directly from host1. Thanks for the pointers ! – pixelastic Aug 15 '11 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .