[ssh_tools] multissh: modernize

This commit is contained in:
Matteo Cypriani 2019-10-21 17:13:32 +02:00
parent eed73bf99f
commit 4f007e1473
1 changed files with 34 additions and 19 deletions

View File

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
# #
# multissh.sh, Copyright © 2013 Matteo Cypriani <mcy@lm7.fr> # multissh.sh, Copyright © 2013, 2019 Matteo Cypriani <mcy@lm7.fr>
# (Formerly named cluster-run.sh) # (Formerly named cluster-run.sh)
# #
# This program is free software. It comes without any warranty, to # This program is free software. It comes without any warranty, to
@ -12,48 +12,63 @@
# Run a command on a list of remote hosts using Parallel SSH. # Run a command on a list of remote hosts using Parallel SSH.
#set -x #set -x
set -u
# Exit codes
readonly EXIT_SUCCESS=0
readonly EXIT_USAGE=127
readonly EXIT_DEPENDENCY=4
readonly EXIT_HOSTS_LIST=6
print_usage() print_usage()
{ {
echo "Usage: $0 [-l login] <hosts_list> <command>" echo "Usage: $0 [-l login] <hosts_list> <command>"
} }
# Do we have at least a host list and a command? bad_usage()
if [ $# -lt 2 ] ; then {
err "$@"
echo >&2
print_usage >&2 print_usage >&2
exit 1 exit $EXIT_USAGE
fi }
err()
{
printf 'Error! ' >&2
printf '%s\n' "$@" >&2
}
# Do we have at least a host list and a command?
[ $# -lt 2 ] && bad_usage "Wrong number of arguments."
# Parse the optional arguments # Parse the optional arguments
LOGIN=
if [ "$1" = "-l" ] ; then if [ "$1" = "-l" ] ; then
LOGIN="$2" LOGIN="$2"
shift shift
shift shift
# Do we still have at least a host list and a command? # Do we still have at least a host list and a command?
if [ $# -lt 2 ] ; then [ $# -lt 2 ] && bad_usage "Wrong number of arguments."
print_usage
exit 3
fi
fi fi
# Check dependencies # Check dependencies
PSSH=$(command -v parallel-ssh || command -v pssh) PSSH=$(command -v parallel-ssh || command -v pssh)
if [ -z "$PSSH" ] ; then if [ -z "$PSSH" ] ; then
echo "Parallel SSH (pssh) is required for this script to work." err "Parallel SSH (pssh) is required for this script to work."
exit 4 exit $EXIT_DEPENDENCY
fi fi
# Hosts' list file # Hosts list file
if [ -z "$XDG_CONFIG_HOME" ] ; then [ -z ${XDG_CONFIG_HOME+x} ] && XDG_CONFIG_HOME="$HOME/.config"
XDG_CONFIG_HOME="$HOME/.config" HOSTS_LIST_NAME="$1"
fi
HOSTS="$XDG_CONFIG_HOME/ssh_tools/$1.lst"
shift shift
echo "Using file '$HOSTS' as hosts' list." HOSTS="$XDG_CONFIG_HOME/ssh_tools/${HOSTS_LIST_NAME}.lst"
echo "Using file '$HOSTS' as hosts list."
if [ ! -f "$HOSTS" ] ; then if [ ! -f "$HOSTS" ] ; then
echo "The hosts' list file doesn't exist or is not a regular file." err "The hosts list file doesn't exist or is not a regular file."
exit 2 exit $EXIT_HOSTS_LIST
fi fi
# Login # Login