RenderDo 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/bin/sh
  2. HostName=render
  3. HostList='1 2 3 4 5 6 7 8 9'
  4. user=wr
  5. ERROR()
  6. {
  7. echo error: $*
  8. exit 1
  9. }
  10. USAGE()
  11. {
  12. [ -z "$1" ] || echo error: $*
  13. echo usage: $(basename "$0") '<options>' command
  14. echo ' --help this message'
  15. echo ' --verbose print more information'
  16. echo ' --pre=cmd a local command for the front of the pipe'
  17. echo ' --post=cmd a local command for the rear of the pipe'
  18. echo ' --yes assume yes'
  19. echo ' --list=i,n-m which machines to run command on'
  20. exit 1
  21. }
  22. ExpandList()
  23. {
  24. local l h i s f
  25. l=$(echo $* | sed 's/[[:space:],;]\{1,\}/ /g')
  26. h=''
  27. for i in ${l}
  28. do
  29. s=${i%%-*}
  30. f=${i##*-}
  31. while [ ${s} -le ${f} ]
  32. do
  33. h="${h} ${s}"
  34. s=$((${s} + 1))
  35. done
  36. done
  37. echo ${h}
  38. }
  39. RunCommands()
  40. {
  41. local user host pre post cmd tag
  42. user="$1"; shift
  43. host="$1"; shift
  44. pre="$1"; shift
  45. post="$1"; shift
  46. case "${verbose}" in
  47. [yY][eE][sS])
  48. [ -n "${pre}" ] && echo local: ${pre} \|
  49. echo ${user}@${host}: $*
  50. [ -n "${post}" ] && echo local: \| ${post}
  51. ;;
  52. *)
  53. case "${brief}" in
  54. [yY][eE][sS])
  55. tag="${user}@${host}: "
  56. ;;
  57. *)
  58. tag=''
  59. ;;
  60. esac
  61. ;;
  62. esac
  63. if [ -z "${pre}" ]
  64. then
  65. if [ -z "${post}" ]
  66. then
  67. ssh -l "${user}" "${host}" "$@" | sed "s/^/${tag}/"
  68. else
  69. ssh -l "${user}" "${host}" "$@" | sed "s/^/${tag}/" | ${post}
  70. fi
  71. else
  72. if [ -z "${post}" ]
  73. then
  74. ${pre} | ssh -l "${user}" "${host}" "$@" | sed "s/^/${tag}/"
  75. else
  76. ${pre} | ssh -l "${user}" "${host}" "$@" | sed "s/^/${tag}/" | ${post}
  77. fi
  78. fi
  79. }
  80. # main program
  81. # ------------
  82. PreCommand=''
  83. PostCommand=''
  84. verbose=no
  85. brief=no
  86. yorn=''
  87. print=no
  88. getopt=/usr/local/bin/getopt
  89. [ -x "${getopt}" ] || getopt=getopt
  90. args=$(${getopt} -o hvbp:o:l:yP --long=help,verbose,brief,pre:,post:,list:,yes,print-list -- "$@") || exit 1
  91. # replace the arguments with the parsed values
  92. eval set -- "${args}"
  93. while :
  94. do
  95. case "$1" in
  96. -v|--verbose)
  97. verbose=yes
  98. shift
  99. ;;
  100. -P|--print-list)
  101. print=yes
  102. shift
  103. ;;
  104. -b|--brief)
  105. brief=yes
  106. shift
  107. ;;
  108. -p|--pre)
  109. PreCommand=$2
  110. shift 2
  111. ;;
  112. -o|--post)
  113. PostCommand=$2
  114. shift 2
  115. ;;
  116. -l|--list)
  117. HostList=$(ExpandList $2)
  118. shift 2
  119. ;;
  120. -y|--yes)
  121. yorn='all'
  122. shift
  123. ;;
  124. --)
  125. shift
  126. break
  127. ;;
  128. -h|--help)
  129. USAGE
  130. ;;
  131. *)
  132. USAGE invalid option: $1
  133. ;;
  134. esac
  135. done
  136. #echo verbose = ${verbose}
  137. #echo ARGS = "$@"
  138. case "${print}" in
  139. [yY]|[yY][eE][sS])
  140. for i in ${HostList}
  141. do
  142. echo ${HostName}${i}
  143. done
  144. exit 0
  145. ;;
  146. *)
  147. ;;
  148. esac
  149. [ -z "$1" ] && USAGE missing command
  150. for i in ${HostList}
  151. do
  152. host="${HostName}${i}"
  153. [ -z "${yorn}" ] && read -p "Execute ${host}: $* (y/n/a/q)? " yorn junk
  154. case "${yorn}" in
  155. [yY]|[yY][eE][sS])
  156. yorn=''
  157. RunCommands "${user}" "${host}" "${PreCommand}" "${PostCommand}" "$@"
  158. ;;
  159. [aA]|[aA][lL][lL])
  160. RunCommands "${user}" "${host}" "${PreCommand}" "${PostCommand}" "$@"
  161. ;;
  162. [qQ]|[qQ][uU][iI][tT])
  163. break
  164. ;;
  165. *)
  166. yorn=''
  167. ;;
  168. esac
  169. done