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

When a use multiple SSH like below i am only able to run one command, if try to run more than one command its fails.

This thing is failing:

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $PEM -t -t $APP_CLUSTER_USER@$APP_CLUSTER_IP "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $PEM -t -t $INSTANCE_USER@$INSTANCE_IP "rm -rf /home/$INSTANCE_USER/$INSTANCE_DEPLOY_DIR ;mkdir $INSTANCE_DEPLOY_DIR"" || exit 1

But breaking the same command into two runs properly.

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $PEM -t -t $APP_CLUSTER_USER@$APP_CLUSTER_IP "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $PEM -t -t $INSTANCE_USER@$INSTANCE_IP "rm -rf /home/$INSTANCE_USER/$INSTANCE_DEPLOY_DIR"" || exit 1

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $PEM -t -t $APP_CLUSTER_USER@$APP_CLUSTER_IP "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $PEM -t -t $INSTANCE_USER@$INSTANCE_IP "mkdir $INSTANCE_DEPLOY_DIR"" || exit 1

Why is this?

share|improve this question

migrated from stackoverflow.com Oct 18 '13 at 12:53

This question came from our site for professional and enthusiast programmers.

up vote 1 down vote accepted

You are trying to nest a command using "" inside another set of "". That doesn't work unless you backslash the inner pair. The way you have it, there are 2 quoted strings:

"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $PEM -t -t $INSTANCE_USER@$INSTANCE_IP "

and

""

and an unquoted series of tokens in the middle:

rm -rf /home/$INSTANCE_USER/$INSTANCE_DEPLOY_DIR ;mkdir $INSTANCE_DEPLOY_DIR

so the semicolon is being interpreted by your original local shell, making the mkdir command entirely separate from the ssh.

The simplified version works by sheer luck. (The deciding factor is that you don't have a semicolon in the unquoted portion, but also you were lucky that ssh doesn't care whether the command you give it is in a single argument or not.)

Change the inner portion to

\"rm -rf /home/$INSTANCE_USER/$INSTANCE_DEPLOY_DIR ;mkdir $INSTANCE_DEPLOY_DIR\"
share|improve this answer

Thanks Kevin, this works.

I have found one more solution to this.

In the nested SSH we should use single quote instead of double quote.

share|improve this answer

You must log in to answer this question.

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