categories de host #3
1 changed files with 47 additions and 26 deletions
added the host tagging functionality, and can handle multiple arguments
commit
0bf73df738
35
mhssh
35
mhssh
|
|
@ -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,9 +44,21 @@ 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
|
||||
case "$1" in
|
||||
while [[ "$#" -gt 0 ]]
|
||||
do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
usage
|
||||
exit 1
|
||||
|
|
@ -66,7 +78,16 @@ case "$1" in
|
|||
shift
|
||||
dtch='on'
|
||||
;;
|
||||
esac
|
||||
-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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue