Last active
January 12, 2016 10:38
-
-
Save allex/6c244e8510eb3a9a30fc to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# ================================================ | |
# Description: A simple repl for cluster intercept | |
# Last Modified: Tue Jan 12, 2016 06:36PM | |
# Author: Allex Wang ([email protected]) | |
# GistID: 6c244e8510eb3a9a30fc | |
# GistURL: https://gist.github.com/6c244e8510eb3a9a30fc | |
# ================================================ | |
dir=`cd $(dirname $0); pwd -P` | |
cdir="$dir/dash.config" | |
user="apps" | |
force= | |
ignore_err= | |
ssh_key="$cdir/id_rsa" | |
ssh_opt="-o ConnectTimeout=5 -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no -i ${ssh_key}" | |
while getopts ":u:h:fi" opt | |
do | |
case $opt in | |
h) hosts="$cdir/$OPTARG" ;; | |
u) user="$OPTARG" ;; | |
f) force=1 ;; | |
i) ignore_err=1 ;; | |
:) echo >&2 "Option -$OPTARG requires an argument." | |
exit 500 | |
;; | |
esac | |
done | |
if [ "$1" = "-h" ]; then | |
shift | |
fi | |
# stty erase ^H | |
# stty kill ^U | |
[ -n "$hosts" ] || hosts="$cdir/default" | |
[ -s $hosts ] || { echo "cluster hosts not found: $hosts"; exit 1; } | |
prompt() { | |
REPLY= | |
read -t 300 -p ">>> " | |
cmd=$REPLY | |
[ $? -eq 0 ] || return; | |
[ -n "$cmd" ] || exit 2; | |
if [[ "$cmd " =~ "rm " ]]; then | |
: | |
# echo "fatal: Command forbid!"; exit 3; | |
fi | |
[ "$cmd" != "q" ] || exit 0; | |
txtred=$(tput setaf 1) | |
txtcyan=$(tput setaf 6) | |
txtrst=$(tput sgr0) | |
ip=$(/sbin/ifconfig |grep inet|grep -v "127.0.0.1"|sed -n '1p'|awk '{print $2}'|awk -F ':' '{print $2}') | |
while read i | |
do | |
if [[ "$i" =~ ^# ]] || [[ "${i}x" = "x" ]]; then | |
continue; | |
fi | |
u="$user" | |
echo "${txtcyan}[$u@$i]${txtrst}$ $cmd"; | |
if [ "$ip" == "$i" ]; then | |
eval "$cmd" | |
else | |
ssh -t -aqTx ${ssh_opt} $u@$i <<ENDSSH | |
set -e; | |
eval "$cmd" | |
ENDSSH | |
fi | |
if [ $? -ne 0 ]; then | |
echo "${txtred}Run command failed.${txtrst}"; | |
[ $ignore_err ] || break; | |
fi | |
done < "$hosts" | |
[ $? -eq 0 ] && echo "Done." && prompt; | |
} | |
echo "Enter command to distribute. (q to exit)" | |
prompt; | |
# vim: set fdm=manual ts=2 sw=2 sts=2 tw=85 et : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment