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