-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonall
executable file
·112 lines (97 loc) · 2.21 KB
/
onall
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#! /usr/bin/env bash
# examples:
# $ ./onall.sh -d bink -e '(layerpro|gremlin|git)' git pull -r
prog="$(basename "$0")"
usage="[-i] [-e regex] [-d directory] [-u user] command args...
-i :: ignore errors
-e :: except machines by regex argument
-d :: cd to 'directory' first
-u :: execute as 'user'
"
set -ueE -o pipefail
function usage {
[ "${*-}" ] && echo "$prog: Error: $*" 1>&2
echo "Usage: $prog: $usage" 1>&2
exit 1
}
function errordie {
[ "${*-}" ] && echo "$prog: Error: $*" 1>&2
exit 1
}
[ "${ONALL_HOSTS-}" ] || errordie ONALL_HOSTS: env var undefined
cd=
ignoreerrors=
except=
user=${USER}
while [ $# -gt 0 ]; do
case $1 in
-d|--cd)
[ $# -lt 2 ] && usage missing companion arg to --cd
shift
cd="cd $1 &&"
;;
-e) except="$2"; shift ;;
-i) ignoreerrors=$1 ;;
--user)
[ $# -lt 2 ] && usage missing companion arg to --user
shift
user="$1"
;;
-*) usage ;;
*) break ;;
esac
shift
done
hosts=${ONALL_HOSTS}
if [ "$except" ]; then
tmp=
for h in $hosts; do
if [[ $h =~ $except ]]; then
echo "Note: skip host $h"
else
tmp="$tmp $h"
fi
done
hosts=$tmp
fi
# from http://stackoverflow.com/questions/6592376/prevent-ssh-from-breaking-up-shell-script-parameters
QUOTE_ARGS=''
for ARG in "$@"; do
ARG=$(printf "%q" "$ARG")
QUOTE_ARGS="${QUOTE_ARGS} $ARG"
done
tempfile="/tmp/${prog}temp$$"
rm -f "$tempfile"
function exit_cleanup {
/bin/rm -f "$tempfile"
}
function err_report {
echo "Error on line $(caller)" 1>&2
}
trap err_report ERR
trap exit_cleanup EXIT
{
for h in $hosts; do
failed=
rm -f "$tempfile"
if [ "${SSH_IDENT-}" ]; then
ssh="ssh -A -i .ssh/${SSH_IDENT}"
else
ssh="ssh -A"
fi
if [ "$ignoreerrors" ]; then
$ssh "${user}@$h" "$cd${QUOTE_ARGS}" &>> "$tempfile" || true
else
if $ssh "${user}@$h" "$cd${QUOTE_ARGS}" &>> "$tempfile"; then
:
else
failed=xxx
fi
fi
if [ "$failed" ] || [ -s "$tempfile" ]; then
echo ""
echo "============= $h ========================================"
[ -s "$tempfile" ] && cat "$tempfile"
fi
done
}