73 lines
1.7 KiB
Plaintext
73 lines
1.7 KiB
Plaintext
|
#!/bin/bash
|
||
|
#MHSSH (Multi Host SSH) : send a command on multiple hosts
|
||
|
#Copyright (C) 2016 Weber Yann
|
||
|
#
|
||
|
#This program is free software; you can redistribute it and/or modify
|
||
|
#it under the terms of the GNU General Public License as published by
|
||
|
#the Free Software Foundation; either version 3 of the License, or
|
||
|
#any later version.
|
||
|
#
|
||
|
#This program is distributed in the hope that it will be useful,
|
||
|
#but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
#GNU General Public License for more details.
|
||
|
#
|
||
|
#You should have received a copy of the GNU General Public License
|
||
|
#along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
|
|
||
|
# Host list path (file should contains hosts seperated by $IFS )
|
||
|
hosts="$HOME/.config/mhssh.hosts"
|
||
|
|
||
|
red=$(tput setaf 1)
|
||
|
grn=$(tput setaf 2)
|
||
|
raz=$(tput sgr0)
|
||
|
|
||
|
usage() {
|
||
|
echo "Run a single command on multiple hosts"
|
||
|
echo "Usage : $0 [-h|--help] [-n|--no-color] CMD ..."
|
||
|
echo -e "\n\t-h --help\tprint this help\n\t-n --no-color\tdisable colors\n\n"
|
||
|
echo "Host list configured to be found in '$hosts'"
|
||
|
}
|
||
|
|
||
|
nocmd() {
|
||
|
echo -e "at least one command expected...\n"
|
||
|
usage
|
||
|
exit 3
|
||
|
}
|
||
|
|
||
|
# arg "parsing"
|
||
|
[ $# -eq 0 ] && nocmd
|
||
|
case "$1" in
|
||
|
-h|--help)
|
||
|
usage
|
||
|
exit 1
|
||
|
;;
|
||
|
-n|--no-color)
|
||
|
[ $# -eq 1 ] && nocmd
|
||
|
shift
|
||
|
red='[ERR] '
|
||
|
grn=''
|
||
|
raz=''
|
||
|
;;
|
||
|
esac
|
||
|
|
||
|
if [ ! -f "$hosts" ]
|
||
|
then
|
||
|
echo -e "File '$hosts' not found" >&2
|
||
|
echo ""
|
||
|
usage
|
||
|
exit 2
|
||
|
fi
|
||
|
|
||
|
|
||
|
for host in $(cat $hosts)
|
||
|
do
|
||
|
echo -e "\n$grn=====================\n$grn$host\n$grn=====================$raz\n"
|
||
|
stdout=$(echo -ne "$grn$(printf "%20s" "$host") : $raz")
|
||
|
stderr=$(echo -ne "$red$(printf "%20s" "$host") : $raz")
|
||
|
{
|
||
|
ssh $host "$@;exit" 2>&3 | sed -e "s/^/$stdout/" 1>&2
|
||
|
} 3>&1 | sed -e "s/^/$stderr/"
|
||
|
done
|