41

I have about a dozen Linux boxes that I occasionally need to run the same command(s) on. Is there an easier way (or automated way) to do this asides from logging on to each machine and running the command, one at a time? It's not the same commands all the time and it's not at a preset time so it's not something ideal for tools like cron.

Nic
  • 13,025
  • 16
  • 59
  • 102
Chris Bunch
  • 979
  • 3
  • 14
  • 17

17 Answers17

15

There's also ClusterSSH, which, well, does exactly what you think it does. Yay for descriptive naming!

Jörg W Mittag
  • 2,108
  • 1
  • 18
  • 17
12

Hm, maybe pconsole is the tool you want.

kaba
  • 113
  • 5
Node
  • 1,644
  • 1
  • 13
  • 15
8

You should check out Puppet here is a linux.com article on its setup and use

trent
  • 3,094
  • 18
  • 17
  • 1
    Hmm, Puppet looks pretty interesting. I'll take a look over it if pconsole starts failing me, since I already have it running and it's easier than learning Puppet's language. Thanks for the suggestion though! – Chris Bunch May 01 '09 at 16:04
  • Specifically, the Marionette Collective tool (also called mcollective, or mco). It's a part of puppet that lets you do things on a group of hosts, based on puppet facts. – Brian Minton Aug 17 '17 at 12:37
8

You may try Capistrano. Originally it is a Rails deployment tool, but it grew to do many things with remote machines.

For a more complete aproach to administration of multiple machine you may want to try Chef.

slhck
  • 315
  • 2
  • 17
rkj
  • 249
  • 1
  • 3
8

Don't overlook pssh and dsh either; they're both much like gsh that @Philip Durbin mentioned.

pjz
  • 10,497
  • 1
  • 31
  • 40
8

Ansible has ability to do ad hoc commands and is also expandable to do full config management. The ability to match based on groups is excellent.

http://docs.ansible.com/intro_adhoc.html

Robert
  • 501
  • 5
  • 14
  • `ansible` works well with hundreds of machines at once or you can limit it to *n* [machines at a time](http://stackoverflow.com/questions/21158689/ansible-deploy-on-multiple-hosts-in-the-same-time) – chicks Oct 19 '15 at 23:41
6

If you are on a KDE desktop, konsole has the "Send Input to all sessions" option. It applies to all the sessions opened in the same konsole window.

rgmarcha
  • 369
  • 1
  • 6
6

I guess I'll copy and paste my answer from Stack Overflow, which people seemed to like. . .


The issue of how to run commands on many servers at once came up on a Perl mailing list the other day and I'll give the same recommendation I gave there, which is to use gsh: http://outflux.net/unix/software/gsh

gsh is similar to the "for box in box1_name box2_name box3_name" solution already given but I find gsh to be more convenient. You set up a /etc/ghosts file containing your servers in groups such as web, db, RHEL4, x86_64, or whatever (man ghosts) then you use that group when you call gsh.

[pdurbin@beamish ~]$ gsh web "cat /etc/redhat-release; uname -r"
www-2.foo.com: Red Hat Enterprise Linux AS release 4 (Nahant Update 7)
www-2.foo.com: 2.6.9-78.0.1.ELsmp
www-3.foo.com: Red Hat Enterprise Linux AS release 4 (Nahant Update 7)
www-3.foo.com: 2.6.9-78.0.1.ELsmp
www-4.foo.com: Red Hat Enterprise Linux Server release 5.2 (Tikanga)
www-4.foo.com: 2.6.18-92.1.13.el5
www-5.foo.com: Red Hat Enterprise Linux Server release 5.2 (Tikanga)
www-5.foo.com: 2.6.18-92.1.13.el5
[pdurbin@beamish ~]$

You can also combine or split ghost groups, using web+db or web-RHEL4, for example.

I'll also mention that while I have never used shmux, its website contains a list of software (including gsh) that lets you run commands on many servers at once. Capistrano has already been mentioned and (from what I understand) could be on that list as well.

Philip Durbin
  • 1,541
  • 2
  • 15
  • 24
5

You could also try ssh [user@]hostname [command] and then loop through the host names.

JasonV.com
  • 711
  • 1
  • 6
  • 6
  • 2
    This doesn't really satisfy the "at once" part (it's serialized, not in parallel), but as that doesn't seem to be a requirement from the question's body this may also be a decent solution. – voretaq7 Dec 19 '11 at 22:00
4

Also take a look at func. Func lets you use a module architecture to control different aspects of a system, from dealing with packages, to running commands, etc. It works on a client/server module over SSL and has a fairly flexible authentication scheme as well.

Stick
  • 658
  • 4
  • 10
  • Along with that, Func is being built by Red Hat, and is likely to see expanded use in future Fedora/RHEL products. This is definitely one to watch, as I expect it will become the de facto standard solution for this type of problem. – Christopher Cashell Jun 12 '09 at 19:13
4

I've used ClusterSSH and it works. However, beyond a certain number of terminals, it doesn't scale well.

I'm surprised no one has mentiond xargs. Xargs is perfect for most things. In fact, I've gotten so comfortable with it that I never use clusterssh anymore.

Example

echo -n host1 host2 host3 host4|xargs -d" " -n1 -P30 -I+ ssh + "sudo service apache2 restart"

(Restart apache on all web hosts.)

JDS
  • 2,508
  • 4
  • 29
  • 48
4

If you are already a tmux user, you might enjoy tmux-cssh (stands for TMUX-C(luster)-SSH). You give it a list of server names and it opens a new tmux window with an SSH session open to each server in its own pane. All keystrokes are copied to each window. Depending on the number of hosts the output can be hard to read.

rogerdpack
  • 575
  • 2
  • 8
  • 22
3

Other tools are:

RunDeck could also be descriped as the lightweight ControlTier.

These tools not only give you a cluster shell, but also a web frontend and you can save your jobs for future use.

Christian
  • 4,645
  • 2
  • 23
  • 27
2

You can also try Cluster SSH (cssh). I haven't used it, but there's an article on LinuxJournal.com called "Manage Multiple Servers Efficiently" that talks about it.

Brad Beyenhof
  • 544
  • 2
  • 7
1

Use dish, simple and very powerful ! This script changed my life :) One script to rule them all !

http://nic-nac-project.org/~drimiks/gnu/dish.shtml

The diligence shell 'dish' executes commands via ssh/rsh/telnet/mysql simultaneously on several systems. Changing password, updating configuration, copying files, checking status, etc. on hundreds of nodes is made then simple. Dish is similar to the IBM's 'dsh' (distributed shell) but furthermore allows remote execution of commands requiring authentication. Compared to other open source packages which include a distributed shell (or similar tools) like ClusterIt, dancer's shell, parallel distributed shell, or C3, dish is a slim and easy to use remote-shell wrapper for cases where a flexible solution is needed. You can browse the dish's manual page on-line here.

Guillaume
  • 136
  • 1
1

Here are some examples using the commands mentioned in other answers:

pdsh

Can take numeric ranges, outputs with the host name first, seems actually maintained:

simplest version:

pdsh -w hostname echo done

output:

hostname: done

More complicated example:

pdsh -w username@hostname[7-8],username@otherhost "sleep 5 && echo done"

output:

hostname8: done
hostname7: done
otherhost: done

mac: brew install pdsh

dsh

dsh -m user@host,user@host --remoteshell ssh --concurrent-shell -- "sleep 5 && echo done"

need remote shell setting otherwise it'll use "rsh" which may or may not even be available.

mac: brew install dsh

pssh

pssh -i -H user@host -H user@host "sleep 5 && echo done"

They can all take list of machines from a file, optionally, as well.

Feel free to add more examples here, it's a comunity wiki

mac: brew install pssh

Cluster SSH

Cluster SSH (the "mac only version" apparently there are two versions the other is here) actually opens "real terminals" with a big terminal at that bottom that sends input to all the others. So it's like a UI that sends it out to multiple.

Ex:

csshX hostname0[1-2] hostname3 then type into the red box and it goes to each terminal window, one per host, that it pops up.

mac: brew install csshx (an old version but still works)

there is also a brew install ansible FWIW

rogerdpack
  • 575
  • 2
  • 8
  • 22
0

Easiest way I found without installing or configuring much software is using plain old tmux. Say you have 9 linux servers. Pick a box as your main. Start a tmux session:

tmux

Then create 9 split tmux panes by doing this 8 times:

ctrl-b + %

enter image description here

Now SSH into each box in each pane. You'll need to know some tmux shortcuts. To navigate, press:

ctrl+b <arrow-keys>

Once your logged in to all your boxes on each pane. Now turn on pane synchronization where it lets you type the same thing into each box:

ctrl+b :setw synchronize-panes on

now when you press any keys, it will show up on every pane. to turn it off, just make on to off. to cycle resize panes, press ctrl+b < space-bar >.

This works alot better for me since I need to see each terminal output as sometimes servers crash or hang for whatever reason when downloading or upgrade software. Any issues, you can just isolate and resolve individually.