Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to be able to locally run a script that will connect to various servers and run commands on them. What is the best way to accomplish this?

share|improve this question

7 Answers 7

Personally, I would use Capistrano. It's friendly and written in Ruby and they already did all of the heavy lifting for you.

From Wikipedia:

Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.

share|improve this answer

you can run a command using ssh hostname command. If you have an entire script you need to execute, first use scp to transfer it to the remote host, then ssh to execute it.

scp script.sh host:script.sh
ssh host ./script.sh
share|improve this answer

What about using configuration management like puppet or chef? This is maybe a little over the top for only one script, but if you need several such scripts it might be worth to consider.

share|improve this answer
2  
I think cfengine falls in this class too –  xenoterracide Aug 17 '10 at 18:38

I would use ssh with key authentication, I believe ssh has a way to make sure that certain accounts can only log in from certain IP's so I would limit it to that because you might not want to set a passphrase on the keys (you could use a key manager to avoid that but it has limitations too)

share|improve this answer
    
Key based authentication, sure. However writing a script that starts with ssh user@host leaves me dropped into the host, blocking execution of the script. I need to remotely execute code. –  Josh K Aug 17 '10 at 17:10
    
@Josh The next argument to ssh is the command, so ssh user@host ls / will list the root directories on that host. See Keith's answer –  Michael Mrozek Aug 17 '10 at 17:56

A quickie bash 'for' loop might be easiest, perhaps something like:

for h in host1 host2 host3
do 
   echo $h
   ssh user@$h "ps -ef"
done

Of course, cfengine/puppet/chef/capistrano are better configuration management options. If you wanted to interactively send commands to the various shells, clusterm (http://sourceforge.net/projects/clusterm/) is a solid choice too.

share|improve this answer

Puppet and Chef are "pull" systems and I've found that a complementary "push" system implemented using Capistrano, Fabric, or ssh(1) in a for-loop is necessary. Of course, that means public keys in place for authentication, too; fortunately, those can be managed by Puppet or Chef.

share|improve this answer

I have been pretty happy with a shell script called dssh.sh that utilizes ssh to communicate with many machines simultaneously. It can execute the same command across lots of machines simultaneously and wait for them all to exit before returning. To download and learn more about it, the best reference I have found is the BASH Cures Cancer blog.

share|improve this answer

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.