Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

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

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

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.