Execute commands simultaneously on multiple servers

401

Author: William von Hagen and Brian K. Jones

If you have multiple servers with similar or identical configurations (such as nodes in a cluster), it’s often difficult to make sure the contents and configuration of those servers are identical. It’s even more difficult when you need to make configuration modifications from the command line, knowing you’ll have to execute the exact same command on a large number of systems (better get coffee first). You could try writing a script to perform the task automatically, but sometimes scripting is overkill for the work to be done. Fortunately, there’s another way to execute commands on multiple hosts simultaneously.

This article is excerpted from Linux Server Hacks, Volume Two, by William von Hagen and Brian K. Jones. Copyright © 2006, O’Reilly Media, Inc. All rights reserved.

A great solution for this problem is an excellent tool called multixterm, which enables you to simultaneously open xterms to any number of systems, type your commands in a single central window and have the commands executed in each of the xterm windows you’ve started. Sound appealing? Type once, execute many — it sounds like a new pipelining instruction set.

multixterm requires expect and tk. The most common way to run multixterm is with a command like multixterm -xc "ssh %n"host1 host2.

Click to enlarge

This command will open ssh connections to host1 and host2. Anything typed in the area labeled “stdin window” (which is usually gray or green, depending on your color scheme) will be sent to both windows, as shown in the figure.

As you can see from the sample command, the -xc option stands for execute command, and it must be followed by the command that you want to execute on each host, enclosed in double quotation marks. If the specified command includes a wildcard such as %n, each hostname that follows the command will be substituted into the command in turn when it is executed. Thus, in our example, the commands ssh host1 and ssh host2 were both executed by multixterm, each within its own xterm window.