Guest User

dap

a guest
Feb 21st, 2010
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.18 KB | None | 0 0
  1. # ANSI color codes
  2. RS="\033[0m"    # reset
  3. HC="\033[1m"    # hicolor
  4. UL="\033[4m"    # underline
  5. INV="\033[7m"   # inverse background and foreground
  6. FBLK="\033[30m" # foreground black
  7. FRED="\033[31m" # foreground red
  8. FGRN="\033[32m" # foreground green
  9. FYEL="\033[33m" # foreground yellow
  10. FBLE="\033[34m" # foreground blue
  11. FMAG="\033[35m" # foreground magenta
  12. FCYN="\033[36m" # foreground cyan
  13. FWHT="\033[37m" # foreground white
  14. BBLK="\033[40m" # background black
  15. BRED="\033[41m" # background red
  16. BGRN="\033[42m" # background green
  17. BYEL="\033[43m" # background yellow
  18. BBLE="\033[44m" # background blue
  19. BMAG="\033[45m" # background magenta
  20. BCYN="\033[46m" # background cyan
  21. BWHT="\033[47m" # background white
  22.  
  23. function gr_usage () {
  24.     echo "gr [-xi] [-g gender[||gender]] [-l piped_localcmd] <interactive_command>"
  25.     echo
  26.     echo " Options:"
  27.     echo "   -x   exclude this node"
  28.     echo "   -i   prompt before action"
  29. }
  30. function gr () {
  31.     local genders=''
  32.     local localcmd=''
  33.     local skipnode=''
  34.     local prompt=''
  35.     OPTIND=1
  36.     local flag
  37.     while getopts "ixg:l:" flag; do
  38.     case "$flag" in
  39.         g) genders="$OPTARG";;
  40.         x) skipnode=`hostname`;;
  41.         l) localcmd="$OPTARG";;
  42.         i) prompt=yes;;
  43.         \?) gr_usage; return;;
  44.     esac
  45.     done
  46.     shift $((OPTIND-1))
  47.    
  48.     if [ -z "$*" ]; then
  49.     gr_usage
  50.     return
  51.     fi
  52.    
  53.     local gender
  54.     if test -z "$genders"; then
  55.     for gender in `nodeattr -l`; do
  56.         if test -n "$genders"; then genders="$genders||"; fi
  57.         genders="$genders$gender"
  58.     done
  59.     fi
  60.     local hosts=''
  61.     for gender in $genders; do
  62.     local host
  63.     for host in `nodeattr -s $gender`; do
  64.         if [ "$skipnode" == $host ]; then continue; fi
  65.         if test -n "$hosts"; then hosts=$hosts,; fi
  66.         hosts=$hosts$host
  67.     done
  68.     done
  69.    
  70.     if ! tty -s; then
  71.     STDIN="`cat`"
  72.     fi
  73.     if test "$prompt" == "yes"; then
  74.     if test -n "$localcmd"; then
  75.         printf "$HC$FYEL$HOSTNAME$RS\$ $HC$WHT$localcmd$RS | {$HC$FYEL$hosts$RS}\$ $HC$WHT$*$RS"
  76.     else
  77.         printf "execute '$HC$FWHT$*$RS' on $HC$FYEL$hosts$RS"
  78.     fi
  79.     echo -n " -- sure? (y/N) "
  80.     read answer
  81.     if [ "$answer" != 'y' -a "$answer" != 'Y' ]; then
  82.         echo "cancelled"
  83.         return
  84.     fi
  85.     fi
  86.     for gender in $genders; do
  87.     for host in `nodeattr -s $gender`; do
  88.         if [ "$skipnode" == $host ]; then
  89.         printf "$FRED### $host ($gender) ###$RS\n"
  90.             continue
  91.         fi
  92.         printf "$HC$FBLE--- $host ($gender) ---$RS\n"
  93.         if test -n "$localcmd"; then
  94.         eval $localcmd | ssh $host "$*"
  95.         else
  96.             ssh -t $host "$*"
  97.         fi
  98.     done
  99.     done
  100. }
  101. alias grx='gr -x'
  102.  
  103. function grcp_usage () {
  104.     echo "grcp [-g gender[||gender]] <../path/to/file> [...]"
  105. }
  106. function grcp () {
  107.     local extra_args=''
  108.     OPTIND=1
  109.     while getopts "g:" flag; do
  110.     case "$flag" in
  111.         g) extra_args="$extra_args -g $OPTARG";;
  112.         \?) grcp_usage; return;;
  113.     esac
  114.     done
  115.     shift $((OPTIND-1))
  116.    
  117.     if test -z "$*"; then
  118.     grcp_usage
  119.     return
  120.     fi
  121.  
  122.     for file in "$@"; do
  123.     local D="`dirname -- \"$file\"`"
  124.     local F="`basename -- \"$file\"`"
  125.     cd "$D"
  126.     if [ ! -f "$F" ]; then
  127.         echo "no such file, skip: $F"
  128.         continue
  129.     fi
  130.     gr -x -i $extra_args -l "cat '$F'" "cat >'$PWD/$F'"
  131.     cd - >/dev/null
  132.     done
  133. }
  134.  
  135. function _gr () {
  136.     local cur prev
  137.  
  138.     COMPREPLY=()
  139.     cur=${COMP_WORDS[COMP_CWORD]}
  140.     prev=${COMP_WORDS[COMP_CWORD-1]}
  141.  
  142.     if [ "$prev" = -g ]; then
  143.         COMPREPLY=($( compgen -W "`nodeattr -l`" -- $cur ))
  144.         return 0
  145.     fi
  146.  
  147.     local word
  148.     local ignore
  149.     local -a COMP_WORDS_NEW
  150.     for word in "${COMP_WORDS[@]}"; do
  151.     if test "$ignore" = yes -a $prev != -l; then
  152.         COMP_POINT=$(( COMP_POINT - ${#word} - 1 ))
  153.         COMP_CWORD=$(( COMP_CWORD - 1 ))
  154.         ignore=no
  155.     else
  156.         case $word in
  157.         -[xilg]*)
  158.             COMP_POINT=$(( COMP_POINT - ${#word} - 1 ))
  159.             COMP_CWORD=$(( COMP_CWORD - 1 ))
  160.             case $word in
  161.             -*[lg]*)
  162.                 ignore=yes ;;
  163.             esac
  164.             ;;
  165.         *)
  166.             COMP_WORDS_NEW=(${COMP_WORDS_NEW[*]} $word)
  167.             ;;
  168.         esac
  169.     fi
  170.     done
  171.  
  172.     COMP_WORDS=( ${COMP_WORDS_NEW[*]} )
  173.     COMP_LINE=${COMP_WORDS_NEW[*]}
  174.     _root_command "${COMP_WORDS_NEW[@]}"
  175. }
  176. complete -F _gr gr grx
  177.  
Advertisement
Add Comment
Please, Sign In to add comment