0

Is it possible to perform a shell operation from a bash script through a secure shell.

Here is an example of why you may want to do this. Lets say you have a simple unix operating system that you need only build and run on, but you want to do all of the development on another machine. I want to write a bash script that has the following functionality:

scp file to location on other machine
ssh to other machine
cd into correct directory 
make 
run program
scp results to file on original computer
exit ssh

Is this remotely possible? (Pardon the Pun :p)

2
  • ssh user1@server1 '/scripts/backup.sh' will run the script on remote machine. You can simply scp "backup.sh" at required location, invoke ssh and exit
    – Usman Saleem
    Dec 17, 2012 at 4:14
  • @UsmanSaleem You should add this as an answer. Dec 17, 2012 at 4:18

3 Answers 3

2

As a direct example of your pseudocode,

scp file remote:/tmp/file
ssh remote 'cd /tmp; cat file >another; rm another'
scp remote:/tmp/another /tmp

Perhaps you want to avoid the use of temprary files; if you can use standard input and standard output for this, you might get away with something like

ssh remote 'remotescriptname' <input >output

... assuming remotescriptname can read standard input, and end up producing the result on standard output. (You will want to prevent noisy commands like make from producing any output on standard output; make -s is your friend.)

You'll notice that you can have an abitrarily complex snippet of shell script between the single quotes. There doesn't need to be a program remotescriptname if you prefer to put the entire command line of the individual commands between the single quotes, like in my first example; but you might want to anyway, because a self-contained script is easier to debug and maintain.

1

Yes, this is possible. For example:

$ echo 'date' | ssh localhost
Pseudo-terminal will not be allocated because stdin is not a terminal.
Password:
Sun Dec 16 22:15:08 CST 2012

I suggest you create a script on the remote side that will do the builds and scp back on success to minimize the amount of remote commands you need to send. Also, making a passwordless RSA key will make your life easier too.

EDIT:

While we are on the topic, you may be interested in a few of these programs.

distcc: Distribute builds across multiple machines

buildbot: Automatically build code as it is checked in on any number of machines/OSes and run unit tests, etc.

1

ssh user1@server1 '/scripts/backup.sh' will run the script on remote machine. You can simply scp "backup.sh" at required location, invoke ssh and exit

1
  • You may copy public keys of your client machine to your server and vice versa so that you don't have to input passwords.
    – Usman Saleem
    Dec 17, 2012 at 4:22

You must log in to answer this question.

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