#!/bin/bash

#
# execute a command via SSH on a cluster of servers.
#

# Setting the CLUSTER environment variable
#
# In bash, you would execute the following at the command line, as an 
# example...
#
# CLUSTER="server1 server2 otheruser@server3 root@server4"
#
# And then; 
#
# export CLUSTER
#
# Otherwise it will use the default file listed below..
CLUSTER_FILE=~/scripts/cluster_exec.data

##set up other variables; 
if [ -x /usr/bin/ssh ]; then
    SSH=/usr/bin/ssh; 
else 
    SSH=`which ssh`;
fi

##Check for an argument.
if [ ! "$1" ]; then
    echo "usage $0 \"<command>\"";
    echo "see script comments for CLUSTER variable description.";
    exit 1;  
fi

##If there is no CLUSTER env variable, define some data.
if [ ! $CLUSTER ]; then
    CLUSTER=`cat $CLUSTER_FILE`; 
fi

##Let's do it!!
for EXECUTE_ON in $CLUSTER
    do
        echo "EXECUTING ON $EXECUTE_ON: \"$1\"";
        $SSH $EXECUTE_ON $1; 
    done