/
RenderDo
executable file
·210 lines (182 loc) · 3.53 KB
/
RenderDo
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/sh
HostName=render
HostList='1 2 3 4 5 6 7 8 9'
user=wr
ERROR()
{
echo error: $*
exit 1
}
USAGE()
{
[ -z "$1" ] || echo error: $*
echo usage: $(basename "$0") '<options>' command
echo ' --help this message'
echo ' --verbose print more information'
echo ' --host=<hostN> set the maximum host name [render]'
echo ' --pre=cmd a local command for the front of the pipe'
echo ' --post=cmd a local command for the rear of the pipe'
echo ' --yes assume yes'
echo ' --list=i,n-m which machines to run command on'
exit 1
}
ExpandList()
{
local l h i s f
l=$(echo $* | sed 's/[[:space:],;]\{1,\}/ /g')
h=''
for i in ${l}
do
s=${i%%-*}
f=${i##*-}
while [ ${s} -le ${f} ]
do
h="${h} ${s}"
s=$((${s} + 1))
done
done
echo ${h}
}
RunCommands()
{
local user host pre post cmd tag
user="$1"; shift
host="$1"; shift
pre="$1"; shift
post="$1"; shift
case "${verbose}" in
[yY][eE][sS])
[ -n "${pre}" ] && echo local: ${pre} \|
echo ${user}@${host}: $*
[ -n "${post}" ] && echo local: \| ${post}
;;
*)
case "${brief}" in
[yY][eE][sS])
tag="${user}@${host}: "
;;
*)
tag=''
;;
esac
;;
esac
if [ -z "${pre}" ]
then
if [ -z "${post}" ]
then
ssh -l "${user}" "${host}" "$@" | sed "s/^/${tag}/"
else
ssh -l "${user}" "${host}" "$@" | sed "s/^/${tag}/" | ${post}
fi
else
if [ -z "${post}" ]
then
${pre} | ssh -l "${user}" "${host}" "$@" | sed "s/^/${tag}/"
else
${pre} | ssh -l "${user}" "${host}" "$@" | sed "s/^/${tag}/" | ${post}
fi
fi
}
# main program
# ------------
PreCommand=''
PostCommand=''
verbose=no
brief=no
yorn=''
print=no
getopt=/usr/local/bin/getopt
[ -x "${getopt}" ] || getopt=getopt
args=$(${getopt} -o hvH:bp:o:l:yP --long=help,verbose,host:,brief,pre:,post:,list:,yes,print-list -- "$@") || exit 1
# replace the arguments with the parsed values
eval set -- "${args}"
while :
do
case "$1" in
-v|--verbose)
verbose=yes
shift
;;
-H|--host)
HostName="${2%%[0-9]*}"
n="${2#${HostName}}"
HostList=''
i=1
while [ ${i} -le ${n} ]
do
HostList="${HostList} ${i}"
i=$((${i} + 1))
done
shift 2
;;
-P|--print-list)
print=yes
shift
;;
-b|--brief)
brief=yes
shift
;;
-p|--pre)
PreCommand=$2
shift 2
;;
-o|--post)
PostCommand=$2
shift 2
;;
-l|--list)
HostList=$(ExpandList $2)
shift 2
;;
-y|--yes)
yorn='all'
shift
;;
--)
shift
break
;;
-h|--help)
USAGE
;;
*)
USAGE invalid option: $1
;;
esac
done
#echo verbose = ${verbose}
#echo ARGS = "$@"
case "${print}" in
[yY]|[yY][eE][sS])
for i in ${HostList}
do
echo ${HostName}${i}
done
exit 0
;;
*)
;;
esac
[ -z "$1" ] && USAGE missing command
for i in ${HostList}
do
host="${HostName}${i}"
[ -z "${yorn}" ] && read -p "Execute ${host}: $* (y/n/a/q)? " yorn junk
case "${yorn}" in
[yY]|[yY][eE][sS])
yorn=''
RunCommands "${user}" "${host}" "${PreCommand}" "${PostCommand}" "$@"
;;
[aA]|[aA][lL][lL])
RunCommands "${user}" "${host}" "${PreCommand}" "${PostCommand}" "$@"
;;
[qQ]|[qQ][uU][iI][tT])
break
;;
*)
yorn=''
;;
esac
done