categories de host #3
1 changed files with 63 additions and 18 deletions
81
mhssh
81
mhssh
|
|
@ -17,17 +17,25 @@
|
|||
|
||||
|
||||
# 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"
|
||||
tags=""
|
||||
|
||||
red=$(tput setaf 1)
|
||||
grn=$(tput setaf 2)
|
||||
raz=$(tput sgr0)
|
||||
|
||||
bgnd=''
|
||||
dtch=''
|
||||
|
||||
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'"
|
||||
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 (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() {
|
||||
|
|
@ -36,21 +44,50 @@ 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
|
||||
-h|--help)
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
-n|--no-color)
|
||||
[ $# -eq 1 ] && nocmd
|
||||
shift
|
||||
red='[ERR] '
|
||||
grn=''
|
||||
raz=''
|
||||
;;
|
||||
esac
|
||||
while [[ "$#" -gt 0 ]]
|
||||
do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
-n|--no-color)
|
||||
[ $# -eq 1 ] && nocmd
|
||||
shift
|
||||
red='[ERR] '
|
||||
grn=''
|
||||
raz=''
|
||||
;;
|
||||
-B|--background)
|
||||
shift
|
||||
bgnd='on'
|
||||
;;
|
||||
-D|--detach)
|
||||
shift
|
||||
dtch='on'
|
||||
;;
|
||||
-t|--tags)
|
||||
shift
|
||||
tags=$1
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done;
|
||||
|
||||
if [ ! -f "$hosts" ]
|
||||
then
|
||||
|
|
@ -61,12 +98,20 @@ 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")
|
||||
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/"
|
||||
done
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue