From f20aa5e864d832884da9922c12e539dd56c8de58 Mon Sep 17 00:00:00 2001 From: Andrew DeFaria Date: Fri, 3 Jun 2016 13:27:41 -0700 Subject: [PATCH] Finally adding allmach. At some point this will be replaced by rexec --- bin/allmach | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100755 bin/allmach diff --git a/bin/allmach b/bin/allmach new file mode 100755 index 0000000..23ed8e5 --- /dev/null +++ b/bin/allmach @@ -0,0 +1,106 @@ +#!/bin/bash +################################################################################ +# +# File: allmach +# Description: Runs an arbitrary command on all machines +# Author: Andrew@DeFaria.com +# Created: Fri Apr 30 14:17:40 PDT 1999 +# Language: Bash Shell +# Modifications:Added trapping of INT so that you can abort a non-responding +# machine. +# +# (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved +# +################################################################################ +# Set me to command name +me=$(basename $0) + +if [ -f ~/.rc/set_colors ]; then + source ~/.rc/set_colors +fi + +# Set adm_base +adm_base=${adm_base:-/opt/clearscm} + +# Set machines +machines=${machines:-$adm_base/data/machines} + +if [ "$1" = "-f" ]; then + shift + machines="$1" + shift +fi + +if [ "$1" = "-r" ]; then + root_ssh=true + shift +fi + +if [ ! -f $machines ]; then + echo "Unable to find $machines file!" + exit 1; +fi + +function trap_intr { + echo "${machines[i]}:$cmd interrupted" + echo -e "$RED(A)bort$NORMAL $me or $YELLOW(C)ontinue$NORMAL with next machine? \c" + read response + typeset -l response=$response + + case "$response" in + a|abort) + echo "Aborting $me..." + exit + ;; + esac + echo "Continuing on with the next machine..." +} # trap_intr + +# Build up data arrays. Note this is done because if we ssh while in a pipe +# Sun will not allow a simple ssh with no command (boo!) +# Column 1 Machine name +# Column 2 Model +# Column 3 OS Version +# Column 4 ClearCase Version (if applicable) +# Column 5 Owner (if known) +# Column 6 Usage (if known) +#oldIFS=$IFS +#IFS=":" +declare -i nbr_of_machines=0 +#sed -e "/^#/d" $machines | +while read machine; do + machines[nbr_of_machines]=$machine + let nbr_of_machines=nbr_of_machines+1 +done < <(grep -v ^# $machines) + +if [[ -z "$@" ]]; then + cmd="# ${YELLOW}<- ssh into machine$NORMAL" +else + cmd="$@" +fi + +# This loop executes the command +trap trap_intr INT +declare -i i=0 +while [ $i -lt $nbr_of_machines ]; do + export currmachine=${machines[i]} + # Execute command. Note if no command is given then the effect is to + # ssh to each machine. + echo -e "${CYAN}${machines[i]}$NORMAL\c" + echo -e ":$cmd" + if [ $# -gt 0 ]; then + if [ "$root_ssh" = "true" ]; then + ssh ${machines[i]} -n -l root "$cmd" + else + ssh ${machines[i]} -n "$cmd" + fi + else + if [ "$root_ssh" = "true" ]; then + ssh ${machines[i]} -l root + else + ssh ${machines[i]} + fi + fi + let i=i+1 +done +trap - INT -- 2.17.1