Multiple remote SSH commands

Hi there,

I am trying to figure out how to write down multiple SSH commands on the remote server. For now I created environment variables to make it less painfull to do a ssh login per command, but I prefer to open one SSH connection and then execute all the commands (more efficient).

My only working solution now is to create separate lines with SSH login and commands:

 - ssh $DEPLOY_USER@$DEPLOY_SERVER tar -xzf $DEPLOY_PATH/deployarchive.tar.gz --directory="$DEPLOY_PATH/folder" 
- ssh $DEPLOY_USER@$DEPLOY_SERVER wget --cache=off --output-document $DEPLOY_PATH/deploy.sh https://a-file-on-the-internet.sh
etc etc
 
I tried some varations (see below), but that leads to build failures as the SSH command is not working (interpreted as one string or command not remotely executed but on docker image)
 
Variation 1
- ssh $DEPLOY_USER@$DEPLOY_SERVER 'tar -xzf $DEPLOY_PATH/deployarchive.tar.gz --directory="$DEPLOY_PATH/folder"; wget --cache=off --output-document $DEPLOY_PATH/deploy.sh https://a-file-on-the-internet.sh;'
Variation 2
 - ssh $DEPLOY_USER@$DEPLOY_SERVER << EOF
              tar -xzf $DEPLOY_PATH/deploy.tar.gz --directory="$DEPLOY_PATH/folder";
              wget --cache=off --output-document $DEPLOY_PATH/deploy.sh https://a-file-on-the-internet.sh;
            EOF;
As I use ENV variables for most commands, I was not able to find a way to include those variables in the remote bash script, otherwise I had to execute only one bashscript to do all the tasks. And besides, when you put multiple commands in 1 bash script, you don't have the advantage of the step by step logging in bitbucket pipelines, as it is interpreted as 1 command.
 
Hope anyone has a solution!
 
Peter
 

2 answers

Didn't you have any reply on this topic? I am trying to do the same, but I need all the commands to run in the same ssh connection :(

No, not yet. We just did a line by line execution. To run all commands in the same SSH connection we ended up by executing a remote .sh script (download and run, see my example with wget).

Oh, I see now, I think I will do something like that so, thanks!

FYI this worked for me:

Example:

ssh $DEPLOY_USER@$DEPLOY_SERVER "command1; command2; if [ $? -ne 0 ]; then echo 'Failed'; exit 1; fi;"

Using "" outside and '' inside.

 

Regards.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 06, 2018 in Bitbucket

Upgrade Best Practices

Hello! My name is Mark Askew and I am a Premier Support Engineer for products Bitbucket Server/Data Center, Fisheye & Crucible. Today, I want to bring the discussion that Jennifer, Matt, and ...

261 views 3 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you