Browse Source

[ssh_tools] multissh: don't require pssh

master
Matteo Cypriani 2 years ago
parent
commit
a2f5e9a48a
  1. 29
      ssh_tools/multissh.sh

29
ssh_tools/multissh.sh

@ -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

Loading…
Cancel
Save