Send the same command to all SSH session

Brian J. Tarricone bjt23 at cornell.edu
Tue Jan 27 07:28:05 CET 2009


Mihamina Rakotomandimby (R12y) wrote:

> [1] People are dumb enough nowadays to confuse mailing list and forum

Just a little nitpick: "forum" is a generic term meaning "public meeting 
place" (cf. the original Latin).  When Auke refers to finding "the 
appropriate forum," he's not using it in the "web forum such as phpBB" 
sense, but in the "collaborative public support resource" sense.

> Many people come to XFCE _from_ another environment. They mostly has
 > their own habits from there and it _is_ on topic to _ask_ if there is
 > an equivalent tool in XFCE.

Sorta.  You're asking if there's a GUI to help automate a command-line 
task, and, yes, there may be one, but Xfce does not provide it.  Other 
environments sometimes take a "kitchen sink" approach and try to cram 
every imaginable feature into their terminal emulator (or whatever), but 
we don't.

Your needs should be served by a simple shell script like this:

#!/bin/bash

SERVERS="myhost1 myhost2 myhost3"
REMOTE_USER=myuser
COMMAND="apt-get update"

for server in $SERVERS; do
    ssh $REMOTE_USER@$server $COMMAND
done

I imagine maintaining the list of hosts in the script file shouldn't be 
too much of a burden, but if it is, there are various options available 
to you so that you can store them in a separate file.

If you want to run all that in parallel (so you can launch the command 
roughly simultaneously on all hosts, rather than in sequence), add a '&' 
to the end of the 'ssh' command line.  Of course, then you might want to 
make the script wait for all commands to complete, so you might replace 
the loop with something like this:

for server in $SERVERS; do
     ssh $REMOTE_USER@$server $COMMAND &
     pids="$pids $!"  # record the process id of each ssh invocation
done

wait $pids  # wait for all of the ssh invocations to complete

Not tested, but I think that should work.  Downside is that you don't 
get any error notification -- if one or more of the hosts are down, it 
will just be silently skipped.  I guess you could replace the 'wait' with:

for pid in $pids; do
     wait $pid || err="$err ???"  # somehow map a PID back to a hostname
done

echo "Failed hosts were: $err"

Of course you'd have to "translate" the PID back into the hostname, an 
exercise left to the reader ^_~.

	-b



More information about the Xfce mailing list