MHSSH (Multi Host SSH) allows you to send a command on multiple hosts with a "pretty" and coloured output.
sh
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mhssh 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash
  2. #MHSSH (Multi Host SSH) : send a command on multiple hosts
  3. #Copyright (C) 2016 Weber Yann
  4. #
  5. #This program is free software; you can redistribute it and/or modify
  6. #it under the terms of the GNU General Public License as published by
  7. #the Free Software Foundation; either version 3 of the License, or
  8. #any later version.
  9. #
  10. #This program is distributed in the hope that it will be useful,
  11. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. #GNU General Public License for more details.
  14. #
  15. #You should have received a copy of the GNU General Public License
  16. #along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. # Host list path (file should contains hosts seperated by $IFS )
  18. hosts="$HOME/.config/mhssh.hosts"
  19. red=$(tput setaf 1)
  20. grn=$(tput setaf 2)
  21. raz=$(tput sgr0)
  22. usage() {
  23. echo "Run a single command on multiple hosts"
  24. echo "Usage : $0 [-h|--help] [-n|--no-color] CMD ..."
  25. echo -e "\n\t-h --help\tprint this help\n\t-n --no-color\tdisable colors\n\n"
  26. echo "Host list configured to be found in '$hosts'"
  27. }
  28. nocmd() {
  29. echo -e "at least one command expected...\n"
  30. usage
  31. exit 3
  32. }
  33. # arg "parsing"
  34. [ $# -eq 0 ] && nocmd
  35. case "$1" in
  36. -h|--help)
  37. usage
  38. exit 1
  39. ;;
  40. -n|--no-color)
  41. [ $# -eq 1 ] && nocmd
  42. shift
  43. red='[ERR] '
  44. grn=''
  45. raz=''
  46. ;;
  47. esac
  48. if [ ! -f "$hosts" ]
  49. then
  50. echo -e "File '$hosts' not found" >&2
  51. echo ""
  52. usage
  53. exit 2
  54. fi
  55. for host in $(cat $hosts)
  56. do
  57. echo -e "\n$grn=====================\n$grn$host\n$grn=====================$raz\n"
  58. stdout=$(echo -ne "$grn$(printf "%20s" "$host") : $raz")
  59. stderr=$(echo -ne "$red$(printf "%20s" "$host") : $raz")
  60. {
  61. ssh $host "$@;exit" 2>&3 | sed -e "s/^/$stdout/" 1>&2
  62. } <&0 3>&1 | sed -e "s/^/$stderr/"
  63. done