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.

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)

share|improve this question

migrated from stackoverflow.com Dec 17 '12 at 10:43

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

    
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 '12 at 4:14
    
@UsmanSaleem You should add this as an answer. –  Ben Dec 17 '12 at 4:18

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer
    
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 '12 at 4:22

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.