categories de host #3

Open
maxime-alves wants to merge 3 commits from (deleted):tagging_hosts into master

81
mhssh
View file

@ -17,17 +17,25 @@
# Host list path (file should contains hosts seperated by $IFS ) # Host list path (file should contains hosts seperated by $IFS )
# You can specify multiple tags [a-z0-9] for each host, so you can filter easier
# which hosts to work on
hosts="$HOME/.config/mhssh.hosts" hosts="$HOME/.config/mhssh.hosts"
tags=""
red=$(tput setaf 1) red=$(tput setaf 1)
grn=$(tput setaf 2) grn=$(tput setaf 2)
raz=$(tput sgr0) raz=$(tput sgr0)
bgnd=''
dtch=''
usage() { usage() {
echo "Run a single command on multiple hosts" echo "Run a single command on multiple hosts"
echo "Usage : $0 [-h|--help] [-n|--no-color] CMD ..." echo "Usage : $0 [-h|--help] [-n|--no-color] [-B|--background] [-D|--detach]
echo -e "\n\t-h --help\tprint this help\n\t-n --no-color\tdisable colors\n\n" [-t|--tags tags] CMD ..."
echo "Host list configured to be found in '$hosts'" echo -e "\n\t-h --help\tprint this help\n\t-n --no-color\tdisable colors\n\t-B --background\tput the task in background\n\t-D --detach\tuse dtach to put the task in background\n\t-t --tags tags (separated by coma)\n\n"
echo "Host list configured to be found in '$hosts', in format \"host.name tag1 ... tagn\""
echo "Tags are not mandatory for mhssh to work"
} }
nocmd() { nocmd() {
@ -36,21 +44,50 @@ nocmd() {
exit 3 exit 3
} }
host_filter() {
if [[ -z "$tags" ]]
then
cat $hosts|cut -d' ' -f1
else
tags="($(echo "$tags"|sed 's/,/\|/g'))"
cat $hosts|grep -E $tags|cut -d' ' -f1
fi
}
# arg "parsing" # arg "parsing"
[ $# -eq 0 ] && nocmd [ $# -eq 0 ] && nocmd
case "$1" in while [[ "$#" -gt 0 ]]
-h|--help) do
usage case "$1" in
exit 1 -h|--help)
;; usage
-n|--no-color) exit 1
[ $# -eq 1 ] && nocmd ;;
shift -n|--no-color)
red='[ERR] ' [ $# -eq 1 ] && nocmd
grn='' shift
raz='' red='[ERR] '
;; grn=''
esac raz=''
;;
-B|--background)
shift
bgnd='on'
;;
-D|--detach)
shift
dtch='on'
;;
-t|--tags)
shift
tags=$1
shift
;;
*)
break
;;
esac
done;
if [ ! -f "$hosts" ] if [ ! -f "$hosts" ]
then then
@ -61,12 +98,20 @@ then
fi fi
for host in $(cat $hosts) for host in $(host_filter)
do do
echo -e "\n$grn=====================\n$grn$host\n$grn=====================$raz\n" echo -e "\n$grn=====================\n$grn$host\n$grn=====================$raz\n"
stdout=$(echo -ne "$grn$(printf "%20s" "$host") : $raz") stdout=$(echo -ne "$grn$(printf "%20s" "$host") : $raz")
stderr=$(echo -ne "$red$(printf "%20s" "$host") : $raz") stderr=$(echo -ne "$red$(printf "%20s" "$host") : $raz")
{ {
ssh $host "$@;exit" 2>&3 | sed -e "s/^/$stdout/" 1>&2 if [ -n "$bgnd" ];
then
ssh $host "nohup $@ > /dev/null < /dev/null & PID=\$!; disown \$PID; echo \$PID; exit" 2>&3 | sed -e "s/^/$stdout/" 1>&2;
elif [ -n "$dtch" ];
then
ssh $host "TMPFILE=$(mktemp -u); dtach -n \$TMPFILE $@; echo \$TMPFILE; exit" 2>&3 | sed -e "s/^/dtach session : $stdout/" 1>&2 ;
else
ssh $host "$@;exit" 2>&3 | sed -e "s/^/$stdout/" 1>&2 ;
fi
} <&0 3>&1 | sed -e "s/^/$stderr/" } <&0 3>&1 | sed -e "s/^/$stderr/"
done done