Enhanced setbg
[clearscm.git] / bin / allmach
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         allmach
5 # Description:  Runs an arbitrary command on all machines
6 # Author:       Andrew@DeFaria.com
7 # Created:      Fri Apr 30 14:17:40 PDT 1999
8 # Language:     Bash Shell
9 # Modifications:Added trapping of INT so that you can abort a non-responding
10 #               machine.
11 #
12 # (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved
13 #
14 ################################################################################
15 # Set me to command name
16 me=$(basename $0)
17
18 if [ -f ~/.rc/set_colors ]; then
19   source ~/.rc/set_colors
20 fi
21
22 # Set adm_base
23 adm_base=${adm_base:-/opt/clearscm}
24
25 # Set machines
26 machines=${machines:-$adm_base/data/machines}
27
28 if [ "$1" = "-f" ]; then
29   shift
30   machines="$1"
31   shift
32 fi
33
34 if [ "$1" = "-r" ]; then
35   root_ssh=true
36   shift
37 fi
38
39 if [ ! -f $machines ]; then
40   echo "Unable to find $machines file!"
41   exit 1;
42 fi
43
44 function trap_intr {
45   echo "${machines[i]}:$cmd interrupted"
46   echo -e "$RED(A)bort$NORMAL $me or $YELLOW(C)ontinue$NORMAL with next machine? \c"
47   read response
48   typeset -l response=$response
49
50   case "$response" in
51     a|abort)
52       echo "Aborting $me..."
53       exit
54     ;;
55   esac
56   echo "Continuing on with the next machine..."
57 } # trap_intr
58
59 # Build up data arrays. Note this is done because if we ssh while in a pipe
60 # Sun will not allow a simple ssh with no command (boo!)
61 # Column 1 Machine name
62 # Column 2 Model
63 # Column 3 OS Version
64 # Column 4 ClearCase Version (if applicable)
65 # Column 5 Owner (if known)
66 # Column 6 Usage (if known)
67 oldIFS=$IFS
68 IFS=":"
69 declare -i nbr_of_machines=0
70 IFS=:
71 while read machine model os cc owner usage; do
72   machines[nbr_of_machines]=$machine
73   let nbr_of_machines=nbr_of_machines+1
74 done < <(grep -v ^# $machines)
75
76 if [[ -z "$@" ]]; then
77   cmd="# ${YELLOW}<- ssh into machine$NORMAL"
78 else
79   cmd="$@"
80 fi
81
82 # This loop executes the command
83 trap trap_intr INT
84 declare -i i=0
85 while [ $i -lt $nbr_of_machines ]; do
86   export currmachine=${machines[i]}
87   # Execute command. Note if no command is given then the effect is to
88   # ssh to each machine.
89   echo -e "${B_AQUA}${machines[i]}$NORMAL\c"
90   echo -e ":$cmd"
91   if [ $# -gt 0 ]; then
92     if [ "$root_ssh" = "true" ]; then
93       ssh ${machines[i]} -n -l root "$cmd"
94     else
95       ssh ${machines[i]} -n "$cmd"
96     fi
97   else
98     if [ "$root_ssh" = "true" ]; then
99       ssh ${machines[i]} -l root
100     else
101       ssh ${machines[i]}
102     fi
103   fi
104   let i=i+1
105 done
106 trap - INT