Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the cleanest way to ssh and run multiple commands in Bash?

Tags:

bash

unix

ssh

I already have an ssh agent set up, and I can run commands on an external server in Bash script doing stuff like:

ssh blah_server "ls; pwd;" 

Now, what I'd really like to do is run a lot of long commands on an external server. Enclosing all of these in between quotation marks would be quite ugly, and I'd really rather avoid ssh'ing multiple times just to avoid this.

So, is there a way I can do this in one go enclosed in parentheses or something? I'm looking for something along the lines of:

ssh blah_server (    ls some_folder;    ./someaction.sh;    pwd; ) 

Basically, I'll be happy with any solution as long as it's clean.

Edit

To clarify, I'm talking about this being part of a larger bash script. Other people might need to deal with the script down the line, so I'd like to keep it clean. I don't want to have a bash script with one line that looks like:

ssh blah_server "ls some_folder; ./someaction.sh 'some params'; pwd; ./some_other_action 'other params';" 

because it is extremely ugly and difficult to read.

like image 549
Eli Avatar asked Dec 10 '10 18:12

Eli


People also ask

How do you chain multiple commands in bash?

Ampersand Operator (&) Sixth Bash shell command line Chaining Operator (Linux operator) is Ampersand Operator (&). Ampersand Operator is a kind of operator which executes given commands in the background. You can use this operator to execute multiple commands at once.

Can you ssh in a bash script?

Bash script SSH is a common tool for Linux users. It is needed when you want to run a command from a local server or a Linux workstation. SSH is also used to access local Bash scripts from a local or remote server.


2 Answers

How about a Bash Here Document:

ssh otherhost << EOF   ls some_folder;    ./someaction.sh 'some params'   pwd   ./some_other_action 'other params' EOF 

To avoid the problems mentioned by @Globalz in the comments, you may be able to (depending what you're doing on the remote site) get away with replacing the first line with

ssh otherhost /bin/bash << EOF 

Note that you can do variable substitution in the Here document, but you may have to deal with quoting issues. For instance, if you quote the "limit string" (ie. EOF in the above), then you can't do variable substitutions. But without quoting the limit string, variables are substituted. For example, if you have defined $NAME above in your shell script, you could do

ssh otherhost /bin/bash << EOF touch "/tmp/${NAME}" EOF 

and it would create a file on the destination otherhost with the name of whatever you'd assigned to $NAME. Other rules about shell script quoting also apply, but are too complicated to go into here.

like image 60
Paul Tomblin Avatar answered Nov 29 '22 04:11

Paul Tomblin


Edit your script locally, then pipe it into ssh, e.g.

cat commands-to-execute-remotely.sh | ssh blah_server 

where commands-to-execute-remotely.sh looks like your list above:

ls some_folder ./someaction.sh pwd; 
like image 21
bosmacs Avatar answered Nov 29 '22 04:11

bosmacs