categories de host #3

Open
maxime-alves wants to merge 3 commits from (deleted):tagging_hosts into master
Showing only changes of commit 0bf73df738 - Show all commits

added the host tagging functionality, and can handle multiple arguments

\/\/ \-/ ❭❬ 2019-11-05 17:26:33 +00:00

31
mhssh
View file

@ -20,6 +20,7 @@
# 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"
tags=""
red=$(tput setaf 1)
grn=$(tput setaf 2)
@ -32,10 +33,9 @@ usage() {
echo "Run a single command on multiple hosts"
echo "Usage : $0 [-h|--help] [-n|--no-color] [-B|--background] [-D|--detach]
[-t|--tags tags] CMD ..."
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\n\n"
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() {
@ -44,8 +44,20 @@ nocmd() {
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"
[ $# -eq 0 ] && nocmd
while [[ "$#" -gt 0 ]]
do
case "$1" in
-h|--help)
usage
@ -66,7 +78,16 @@ case "$1" in
shift
dtch='on'
;;
-t|--tags)
shift
tags=$1
shift
;;
*)
break
;;
esac
done;
if [ ! -f "$hosts" ]
then
@ -77,7 +98,7 @@ then
fi
for host in $(cat $hosts)
for host in $(host_filter)
do
echo -e "\n$grn=====================\n$grn$host\n$grn=====================$raz\n"
stdout=$(echo -ne "$grn$(printf "%20s" "$host") : $raz")