

if [ $# -eq 0 ] 
then 
echo $0 requires at least one parameter.
$0 -h
exit 1
fi


set -- `getopt hn:aq $*`
asyn=""
quiet=0
hosts=""

while [ ! $1 = "--" ]
do
case $1 in
-h)
cat <<EOM

mssh ,  = multi ssh

$0 is a wrapper to execute the same command at a number of ssh capable hosts.
including your windows box with cygwin, if you wish.... or ssh enabled powershell windows boxen....

it wraps ssh. so read the ssh manpage, as well.....

if you setup pub/priv key on these hosts, you will enjoy not having to enter the password for every host involved..... however, that implies you know what you are doing....

usage:
$0 -h = this help

flags : -n host1 -n user0@host2 -n user1@host3 -n host:portnr = specify hosts on the commandline, as follows:  <optionaluser@>hostname<:optional portnr>

flags : -a = asynchronous. command returns immediately, to prevent from hanging on a
host that is down.

flags : -q  = quiet. output is NOT prepended with the hostname where it came from. 
useful sometimes in case of a single host, otherwise unpractical...

$0 creates a ~/.mssh directore, and in it ~/.mssh/mssh.hosts
this file (mssh.hosts) can contain default hosts to execute on, if none is given.

usage: 3 ways :  
1) $0 -- [ command ] = this uses the mssh.hosts file for hostnames to execute on.

2) $0 -n somehostname -- [ command ] = this uses somehostname (e.g: root@host1 or host1 )

3) $0 customfile -- [ command ] = this uses ~/.mssh/customfile as the source of hostnames to execute on.

WARNING !!!!!   ALWAYS use "--" after flags etc. otherwise flags in the command will be stripped from your command. DO or let your hosts DIE (in a irrevocable way, probably...)

BE CAREFUL WITH THIS TOOL. IT IS VERY POWERFUL

please note: $0 requires at least one parameter.

EOM
exit 1

;;
-n) 
# make hosts list...
hosts=$hosts" "$2
shift 1
 ;;
-a) 
asyn="-f"
 ;;
 -q)
 quiet=1
 ;;
esac
shift 1
done
shift 1
echo parms : $*
echo flags: hosts = $hosts asyn = $asyn quiet = $quiet
echo \$1 = $1
# read niks

outcmd=""
if [ $hosts"" = "" ]
then
outcmd=cat
if [ -f ~/.mssh/`basename $1` ] 
then
 hosts=~/.mssh/`basename $1` 
 shift 1
 outcmd=cat
else
hosts=~/.mssh/`basename $0`.hosts
fi
echo parms $*
echo ========= hosts file : $hosts
cat $hosts|grep -v ^$
else # hosts var not empty...
outcmd=echo
fi # hosts var is empty...

echo ===== start loop

for host in ` $outcmd $hosts|grep -v -e ^$ -e ^#  `
do
#echo ==== before host $host node $node
# ssh $host uname -a

# replace :portnumber, if any, with "-p "portnumber ssh syntax...
 node=" `echo $host|sed 's/:/ -p /g'|grep -v ^$`"
# echo ==== after  host $host node $node
echo +++++++++++++  ssh $node $*
# ssh $node $*
# echo $cmd "$*"
# eval $cmd "$*"   

case $quiet in
0)
# echo "---- not quiet"
 ssh  $asyn $node $* |awk -v host=$host '{print host":"$0}' 
;;
1)
# echo "---- quiet"
 ssh $asyn $node "$*"
;;
esac
done
