-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcmultido
executable file
·58 lines (46 loc) · 1.24 KB
/
cmultido
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
#!/bin/sh
# copyright 2008 Mochi Media, Inc.
# example:
# ./cmultido host1:host2:host3 ls /tmp
exec chpst -P bash -c '
SSH=${SSH:-ssh}
user=$USER;
sync="false";
while getopts u:s opt; do
case "$opt" in
u) user="$OPTARG";;
s) sync="true";;
\?) echo >&2 \
"usage: $0 [-u USER] [-s]"
exit 1;;
esac
done
shift `expr $OPTIND - 1`
hosts="$1"; shift
cleanup() {
kill 0
}
ALT_HOST_COLOR_BASES=(
"0;32m" "0;36m" "0;35m" "0;33m" "1;32m" "1:36m" "1;35m"
)
no_color="\033[0m"
max=0
for host in $(echo "$hosts" | tr : " "); do
x=`echo $host | wc -c`
[ "$x" -gt "$max" ] && max="$x"
done
for host in $(echo "$hosts" | tr : " "); do
hostname=$(dig +short -x $host | cut -d"." -f1)
hash=`python -c "print abs(hash(\"${host}\"))"`
base=`expr ${hash} % ${#ALT_HOST_COLOR_BASES} + 1`
host_color="\033[${ALT_HOST_COLOR_BASES[${base}]}"
padded=`printf "%-${max}s" "$hostname($host)"`
if [[ "$sync" == "true" ]]; then
( ${SSH} -t "${user}@${host}" "$@" 2>&1 | sed -e "s/^/$(echo -e $host_color)$padded: /" -e "s/$/$(echo -e $no_color)/" );
else
( ${SSH} -t "${user}@${host}" "$@" 2>&1 | sed -e "s/^/$(echo -e $host_color)$padded: /" -e "s/$/$(echo -e $no_color)/" ) &
fi;
done
trap cleanup INT TERM
wait
' "$0" "$@"