Created
March 23, 2012 03:29
-
-
Save chimerast/2166484 to your computer and use it in GitHub Desktop.
複数のSSHサーバに同じコマンドを送って実行結果を出力する (非同期可)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
dupsh $@ <<EOF | |
10.101.10.12 | |
10.101.10.13 | |
10.101.10.15 | |
10.101.10.24 | |
10.101.10.28 | |
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -z "$1" ]; then | |
echo "usage: $0 <command> [args] ..." | |
exit; | |
fi | |
# setup destination | |
while read LINE; do | |
if [ $LINE = ${LINE#\#} ]; then | |
NODES=(${NODES[@]} $LINE) | |
fi | |
done | |
CPIDS=() | |
echo CMD_RSH=${CMD_RSH:=ssh} | |
echo USER=${USER:=root} | |
echo SYNC=${SYNC:=1} | |
echo COLOR=${COLOR:=1} | |
echo NODES=${NODES[@]} | |
# output dir | |
LOGDIR=/tmp/dupsh_$USER_$$ | |
mkdir -p $LOGDIR | |
if [ $SYNC -ne 0 ]; then | |
# execute in child process (sync) | |
for NODE in ${NODES[@]}; do | |
echo -e "\033[0;32m[$USER@$NODE]$ \033[0;31m$CMD_RSH $USER@$NODE eval \"$@\"\033[0m" | |
$CMD_RSH $USER@$NODE eval "$@" | |
done | |
else | |
# execute in child process (async) | |
for NODE in ${NODES[@]}; do | |
LOG=$LOGDIR/$NODE | |
echo -e "\033[0;32m[$USER@$NODE]$ \033[0;31m$CMD_RSH $USER@$NODE eval \"$@\"\033[0m" > $LOG | |
$CMD_RSH $USER@$NODE eval "$@" >> $LOG 2>&1 & | |
CPIDS=(${CPIDS[@]} $!) | |
done | |
# wait for all child process | |
for CPID in ${CPIDS[@]}; do | |
wait $CPID | |
done | |
# output result | |
for NODE in ${NODES[@]}; do | |
LOG=$LOGDIR/$NODE | |
cat $LOG | |
rm $LOG | |
done | |
fi | |
rm -rf $LOGDIR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment