scripts/ssh_tools/multissh.sh

68 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
#
# cluster-run.sh, Copyright © 2013 Matteo Cypriani <mcy@lm7.fr>
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#
# Run a command on a list of remote hosts using Parallel SSH.
#set -x
print_usage()
{
echo "Usage: $0 [-l login] <hosts_list> <command>"
}
# Do we have at least a host list and a command?
if [ $# -lt 2 ] ; then
print_usage >&2
exit 1
fi
# Parse the optional arguments
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
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
fi
# Hosts' list file
if [ -z "$XDG_CONFIG_HOME" ] ; then
XDG_CONFIG_HOME="$HOME/.config"
fi
HOSTS="$XDG_CONFIG_HOME/cluster/$1.lst"
shift
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
fi
# Login
if [ -n "$LOGIN" ] ; then
echo "Login: $LOGIN"
LOGIN="-l $LOGIN"
fi
echo "Command:" "$@"
# shellcheck disable=SC2086
exec $PSSH --print $LOGIN -h "$HOSTS" -- "$@"