#!/bin/sh
#-----------------------------------------------------------------------
#;  Copyright (C) 1995, 1997
#;  Associated Universities, Inc. Washington DC, USA.
#;
#;  This program is free software; you can redistribute it and/or
#;  modify it under the terms of the GNU General Public License as
#;  published by the Free Software Foundation; either version 2 of
#;  the License, or (at your option) any later version.
#;
#;  This program is distributed in the hope that it will be useful,
#;  but WITHOUT ANY WARRANTY; without even the implied warranty of
#;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#;  GNU General Public License for more details.
#;
#;  You should have received a copy of the GNU General Public
#;  License along with this program; if not, write to the Free
#;  Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
#;  MA 02139, USA.
#;
#;  Correspondence concerning AIPS should be addressed as follows:
#;         Internet email: aipsmail@nrao.edu.
#;         Postal address: AIPS Project Office
#;                         National Radio Astronomy Observatory
#;                         520 Edgemont Road
#;                         Charlottesville, VA 22903-2475 USA
#-----------------------------------------------------------------------
# Usage: ALLRSH [-s] "command"
#-----------------------------------------------------------------------
# ALLRSH executes a command on each host listed in logical HOSTS.
#
# Initial version: MRC 91/May/29
# Modified:        PPM 93/Jan/27 (Posix: remsh)
# Modified:        PPM 97/Oct/29 (ssh allowed too
#-----------------------------------------------------------------------
#                                       Check command to be executed.
usessh=""
if [ "$1" = "-s" ] ; then
   usessh="ssh"
   ( IFS=$IFS:
     for i in $PATH ; do
        [ -f $i/ssh ] && found=$i && break
     done
     if [ "$found" = "" ] ; then
        echo "Cannot find ssh anywhere in your PATH"
        exit 1
     fi
   )
   shift
fi
if [ "$*" = "" ] ; then
   echo "No command to execute"
   exit
fi

#                                       Check hostnames.
  if [ "$HOSTS" = "" ] ; then
   echo "HOSTS undefined"
   exit
fi

#                                       Convert to lower case.
  HOSTS=`echo $HOSTS | tr '[A-Z]' '[a-z]'`

  echo "Execute:  $*"
  echo "On hosts: $HOSTS"

#                                       rsh or remsh?
remcmd="rsh"
case $ARCH in
   HP*) remcmd="remsh";;
esac
[ "$usessh" != "" ] && remcmd=ssh

#                                       Loop for each host.
for HOST in $HOSTS ; do
   echo ""
   echo "$HOST"
   if ping $HOST 2 >/dev/null ; then
      $remcmd $HOST "$*"
   fi
done
