Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to run a sript that is present in local machine, but it should perform operations on remote machine. I have already generated public-private keys, so no password authentiaction is required

share|improve this question
1  
why don't you just copy your script to remote machine? –  Paweł Karpiński Feb 14 '11 at 11:50
    
Lots of reasons. Say you need to scrape data from fifty machines. If you ever change the script, which you rather worry about pushing changes to fifty machines, or just edit your one central script? –  djeikyb Feb 14 '11 at 12:49
add comment

2 Answers

ssh user@host 'sh' < your_script.sh

First of all, this command is a redirection: your shell will open the file your_script.sh and feed it as input to the ssh command. ssh, in turn, will tunnel its stdin to the remote command, namely, sh instance. And sh without arguments reads its script from stdin. So we got sh instance, which is launched on remote host, but reads commands from your local file. Voila!

share|improve this answer
add comment

This SO answers your exact question:bash - How do you use ssh in a shell script? - Stack Overflow

ssh user@host command
#for example
ssh user@host ls

If you have a big script which you want to execute. Then you can copy the script to the remote machine using sftp and then execute it via the above command.

share|improve this answer
    
You can even use the stdout of the remote command, like: ssh user@host command > localfile.txt or ssh user@host command | something | something2 ... –  LGB Feb 14 '11 at 12:42
add comment

Your Answer

 
discard

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