[ssh_tools] multissh: don't require pssh

This commit is contained in:
Matteo Cypriani 2019-10-21 17:51:31 +02:00
parent 4f007e1473
commit a2f5e9a48a
1 changed files with 24 additions and 5 deletions

View File

@ -17,7 +17,6 @@ set -u
# Exit codes
readonly EXIT_SUCCESS=0
readonly EXIT_USAGE=127
readonly EXIT_DEPENDENCY=4
readonly EXIT_HOSTS_LIST=6
print_usage()
@ -39,6 +38,23 @@ err()
printf '%s\n' "$@" >&2
}
warn()
{
printf 'Warning! ' >&2
printf '%s\n' "$@" >&2
}
ssh_command()
{
grep -v '^#' "$HOSTS" | while read -r host ; do
[ -z "$host" ] && continue
echo "Executing command on '$host'..."
# shellcheck disable=SC2086
ssh ${LOGIN} "${host}" -- "$@" </dev/null
# Note: </dev/null is to prevent ssh from swallowing stdin (see SC2095).
done
}
# Do we have at least a host list and a command?
[ $# -lt 2 ] && bad_usage "Wrong number of arguments."
@ -56,8 +72,7 @@ fi
# Check dependencies
PSSH=$(command -v parallel-ssh || command -v pssh)
if [ -z "$PSSH" ] ; then
err "Parallel SSH (pssh) is required for this script to work."
exit $EXIT_DEPENDENCY
warn "Parallel SSH (pssh) is not available. Install it for best results."
fi
# Hosts list file
@ -79,5 +94,9 @@ fi
echo "Command:" "$@"
# shellcheck disable=SC2086
exec $PSSH --print $LOGIN -h "$HOSTS" -- "$@"
if [ -n "$PSSH" ] ; then
# shellcheck disable=SC2086
exec $PSSH --print $LOGIN -h "$HOSTS" -- "$@"
else
ssh_command "$@"
fi