1

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?

2
  • 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. Jan 18, 2011 at 23:24
  • Please use appropriate tags so that others know what systems you're talking about. Jan 19, 2011 at 0:52

3 Answers 3

6

That's an easy one:

ssh user@host command

command = elaborate with piping and such as needed.

2
  • 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, 2011 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, 2011 at 23:36
4

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!)
1
  • +1 for RSA key instructions
    – danlefree
    Jan 18, 2011 at 23:39
1

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.

You must log in to answer this question.

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