Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ANSI color codes
- RS="\033[0m" # reset
- HC="\033[1m" # hicolor
- UL="\033[4m" # underline
- INV="\033[7m" # inverse background and foreground
- FBLK="\033[30m" # foreground black
- FRED="\033[31m" # foreground red
- FGRN="\033[32m" # foreground green
- FYEL="\033[33m" # foreground yellow
- FBLE="\033[34m" # foreground blue
- FMAG="\033[35m" # foreground magenta
- FCYN="\033[36m" # foreground cyan
- FWHT="\033[37m" # foreground white
- BBLK="\033[40m" # background black
- BRED="\033[41m" # background red
- BGRN="\033[42m" # background green
- BYEL="\033[43m" # background yellow
- BBLE="\033[44m" # background blue
- BMAG="\033[45m" # background magenta
- BCYN="\033[46m" # background cyan
- BWHT="\033[47m" # background white
- function gr_usage () {
- echo "gr [-xi] [-g gender[||gender]] [-l piped_localcmd] <interactive_command>"
- echo
- echo " Options:"
- echo " -x exclude this node"
- echo " -i prompt before action"
- }
- function gr () {
- local genders=''
- local localcmd=''
- local skipnode=''
- local prompt=''
- OPTIND=1
- local flag
- while getopts "ixg:l:" flag; do
- case "$flag" in
- g) genders="$OPTARG";;
- x) skipnode=`hostname`;;
- l) localcmd="$OPTARG";;
- i) prompt=yes;;
- \?) gr_usage; return;;
- esac
- done
- shift $((OPTIND-1))
- if [ -z "$*" ]; then
- gr_usage
- return
- fi
- local gender
- if test -z "$genders"; then
- for gender in `nodeattr -l`; do
- if test -n "$genders"; then genders="$genders||"; fi
- genders="$genders$gender"
- done
- fi
- local hosts=''
- for gender in $genders; do
- local host
- for host in `nodeattr -s $gender`; do
- if [ "$skipnode" == $host ]; then continue; fi
- if test -n "$hosts"; then hosts=$hosts,; fi
- hosts=$hosts$host
- done
- done
- if ! tty -s; then
- STDIN="`cat`"
- fi
- if test "$prompt" == "yes"; then
- if test -n "$localcmd"; then
- printf "$HC$FYEL$HOSTNAME$RS\$ $HC$WHT$localcmd$RS | {$HC$FYEL$hosts$RS}\$ $HC$WHT$*$RS"
- else
- printf "execute '$HC$FWHT$*$RS' on $HC$FYEL$hosts$RS"
- fi
- echo -n " -- sure? (y/N) "
- read answer
- if [ "$answer" != 'y' -a "$answer" != 'Y' ]; then
- echo "cancelled"
- return
- fi
- fi
- for gender in $genders; do
- for host in `nodeattr -s $gender`; do
- if [ "$skipnode" == $host ]; then
- printf "$FRED### $host ($gender) ###$RS\n"
- continue
- fi
- printf "$HC$FBLE--- $host ($gender) ---$RS\n"
- if test -n "$localcmd"; then
- eval $localcmd | ssh $host "$*"
- else
- ssh -t $host "$*"
- fi
- done
- done
- }
- alias grx='gr -x'
- function grcp_usage () {
- echo "grcp [-g gender[||gender]] <../path/to/file> [...]"
- }
- function grcp () {
- local extra_args=''
- OPTIND=1
- while getopts "g:" flag; do
- case "$flag" in
- g) extra_args="$extra_args -g $OPTARG";;
- \?) grcp_usage; return;;
- esac
- done
- shift $((OPTIND-1))
- if test -z "$*"; then
- grcp_usage
- return
- fi
- for file in "$@"; do
- local D="`dirname -- \"$file\"`"
- local F="`basename -- \"$file\"`"
- cd "$D"
- if [ ! -f "$F" ]; then
- echo "no such file, skip: $F"
- continue
- fi
- gr -x -i $extra_args -l "cat '$F'" "cat >'$PWD/$F'"
- cd - >/dev/null
- done
- }
- function _gr () {
- local cur prev
- COMPREPLY=()
- cur=${COMP_WORDS[COMP_CWORD]}
- prev=${COMP_WORDS[COMP_CWORD-1]}
- if [ "$prev" = -g ]; then
- COMPREPLY=($( compgen -W "`nodeattr -l`" -- $cur ))
- return 0
- fi
- local word
- local ignore
- local -a COMP_WORDS_NEW
- for word in "${COMP_WORDS[@]}"; do
- if test "$ignore" = yes -a $prev != -l; then
- COMP_POINT=$(( COMP_POINT - ${#word} - 1 ))
- COMP_CWORD=$(( COMP_CWORD - 1 ))
- ignore=no
- else
- case $word in
- -[xilg]*)
- COMP_POINT=$(( COMP_POINT - ${#word} - 1 ))
- COMP_CWORD=$(( COMP_CWORD - 1 ))
- case $word in
- -*[lg]*)
- ignore=yes ;;
- esac
- ;;
- *)
- COMP_WORDS_NEW=(${COMP_WORDS_NEW[*]} $word)
- ;;
- esac
- fi
- done
- COMP_WORDS=( ${COMP_WORDS_NEW[*]} )
- COMP_LINE=${COMP_WORDS_NEW[*]}
- _root_command "${COMP_WORDS_NEW[@]}"
- }
- complete -F _gr gr grx
Advertisement
Add Comment
Please, Sign In to add comment