diff --git a/ssh_tools/multissh.sh b/ssh_tools/multissh.sh index 880af3f..725026e 100755 --- a/ssh_tools/multissh.sh +++ b/ssh_tools/multissh.sh @@ -1,6 +1,6 @@ #!/bin/sh # -# multissh.sh, Copyright © 2013 Matteo Cypriani +# multissh.sh, Copyright © 2013, 2019 Matteo Cypriani # (Formerly named cluster-run.sh) # # 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. #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() { echo "Usage: $0 [-l login] " } -# Do we have at least a host list and a command? -if [ $# -lt 2 ] ; then +bad_usage() +{ + err "$@" + echo >&2 print_usage >&2 - exit 1 -fi + exit $EXIT_USAGE +} + +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 +LOGIN= if [ "$1" = "-l" ] ; then LOGIN="$2" shift shift # Do we still have at least a host list and a command? - if [ $# -lt 2 ] ; then - print_usage - exit 3 - fi + [ $# -lt 2 ] && bad_usage "Wrong number of arguments." fi # Check dependencies PSSH=$(command -v parallel-ssh || command -v pssh) if [ -z "$PSSH" ] ; then - echo "Parallel SSH (pssh) is required for this script to work." - exit 4 + err "Parallel SSH (pssh) is required for this script to work." + exit $EXIT_DEPENDENCY fi -# Hosts' list file -if [ -z "$XDG_CONFIG_HOME" ] ; then - XDG_CONFIG_HOME="$HOME/.config" -fi -HOSTS="$XDG_CONFIG_HOME/ssh_tools/$1.lst" +# Hosts list file +[ -z ${XDG_CONFIG_HOME+x} ] && XDG_CONFIG_HOME="$HOME/.config" +HOSTS_LIST_NAME="$1" 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 - echo "The hosts' list file doesn't exist or is not a regular file." - exit 2 + err "The hosts list file doesn't exist or is not a regular file." + exit $EXIT_HOSTS_LIST fi # Login