Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I feel a little silly asking what would seem to be a Google-able question but I'm trying to script out a repetitive task of (1) ssh'ing into a remote server (2) running script.sh in my home directory, and (3) copy/pasting the output.

It would be great if I could write a script to do this work for me. I've written some bash scripts that scp files from these machines but never one that ran scripts on these machines.

Is it possible to have a script on machine 1 and log in and execute script.sh on machine 2 through machine n, dumping the output on machine 1? If so, how?

share|improve this question
1  
Your thinking is too complicated. Copy and paste are things you do in a GUI. When scripting, avoid copying as much as you can. –  reinierpost Jan 18 '11 at 23:24
    
Please use appropriate tags so that others know what systems you're talking about. –  John Gardeniers Jan 19 '11 at 0:52
    
Thanks for the reminder. I've added a couple tags. –  editor Jan 20 '11 at 0:13

3 Answers 3

up vote 6 down vote accepted

That's an easy one:

ssh user@host command

command = elaborate with piping and such as needed.

share|improve this answer
    
Thank you for this. It should have been an easy one for me too, but strangely I couldn't seem to form the query to find it. –  editor Jan 18 '11 at 23:26
    
No worries! Of course, it can get very complicated real fast once you elaborate your command, but it doesn't need to be! Goodluck! –  Publiccert Jan 18 '11 at 23:36

So I think the real question here is how do you get the script from machine 1 to machine 2 so you can execute it, right? There are a number of ways to do that. You could use a script on machine 1 that first uses scp to transfer the scriptfile script to machine 2, and then runs ssh machine2 script.

That works well if you have public key auth all set up consistently so you don't get prompted for passwords. If you need to type your password each time you log in to machine2, then you need to look at approaches using expect, as detailed in this stackoverflow posting. You should be able to use autoexpect to write the necessary expect script quickly.

Other approaches include nfs-mounting a shared directory on all the hosts. In that scenario, you copy a scriptfile to that directory on machine1, and it's then available on machine2 and all other machines connected to that same nfs mount.

Finally this is a problem that configuration management tools like cfengine and puppet solve with some sort of file delivery mechanism. In that setup, client machines connect to a central server and download the scriptfile before executing it.

share|improve this answer

Similar to Jeffrey's answer, but with a one-liner loop.

for i in "server1 server2 otherserver"; do ( ssh username@$i yourcommand & ); done

This lets you spawn each task backgrounded, so the whole command will just take a moment from your workstation. If you're using password-based authentication, you'll need to either:

  • Implement password-less login (preferred!)
  • Get rid of the ampersand (&) and enter the password each time
  • Specify the password on the command line (only included for completeness -- please don't!)
share|improve this answer
    
+1 for RSA key instructions –  danlefree Jan 18 '11 at 23:39

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.