Last active
October 12, 2015 15:41
-
-
Save allaryin/dceb768e79fe4ad72f19 to your computer and use it in GitHub Desktop.
Script to shotgun a command to a list of servers via 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/sh | |
#HOSTS="" | |
HOSTS=`awk '/^(Host|## END)/ {if ($0 == "## END SSH-ALL ##") {exit 0;} else {$1=""; print $0;}}' ~/.ssh/config` | |
HOSTLIST=`mktemp hostlist.XXXXXX` | |
for HOST in ${HOSTS}; do | |
echo ${HOST} >> ${HOSTLIST} | |
done | |
HOSTS=`cat ${HOSTLIST} | sort` | |
rm ${HOSTLIST} | |
MAX_TERMS=4 | |
if [ "$1" == "dumphosts" ]; then | |
echo $HOSTS | |
exit | |
fi | |
########## | |
if [ "$2" == "nofork" ]; then | |
FORK=0 | |
else | |
FORK=1 | |
fi | |
if [ "$2" == "pause" ]; then | |
PAUSE="read -n 1" | |
else | |
PAUSE="" | |
fi | |
SCRIPT="$1" | |
if [ -f "${SCRIPT}" ]; then | |
chmod +x ${SCRIPT} | |
fi | |
NUM_TERMS=0 | |
TERM_PIDS="" | |
for HOST in ${HOSTS}; do | |
echo | |
echo "---------- $HOST ----------" | |
if [ -f "${SCRIPT}" ]; then | |
scp ${SCRIPT} ${HOST}: | |
if [ $FORK -eq 1 ]; then | |
xterm -T "${HOST}" -rv -e "ssh -t ${HOST} \"sudo ./${SCRIPT};${PAUSE}\"" & | |
TERM_PIDS="$! $TERM_PIDS" | |
(( NUM_TERMS++ )) | |
if [ $NUM_TERMS -ge $MAX_TERMS ]; then | |
for TERM in ${TERM_PIDS}; do | |
wait $TERM | |
done | |
TERM_PIDS="" | |
NUM_TERMS=0 | |
fi | |
else | |
ssh -t ${HOST} "sudo ./${SCRIPT}" | |
fi | |
else | |
ssh -t ${HOST} $* | |
${PAUSE} | |
fi | |
if [ -f "~/.stop-ssh" ]; then | |
rm ~/.stop-ssh | |
break | |
fi | |
done | |
echo "----------" | |
echo "done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment