#!/bin/sh # # cluster-run.sh, Copyright © 2013 Matteo Cypriani # # 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] " } # Parse the optional arguments with getopt export POSIXLY_CORRECT=yes export GETOPT_COMPATIBLE=yes OPTSTRING="l:" ARGS=$(getopt $OPTSTRING $*) if [ $? -ne 0 ] ; then print_usage exit 1 fi set -- $ARGS while [ $# -ne 0 ] ; do case "$1" in -l) LOGIN="$2" shift shift ;; --) shift break ;; *) echo "Unknown argument:" $1 exit 2 esac done # Do we have at least a host list and a command? if [ $# -lt 2 ] ; then print_usage exit 3 fi # Check dependencies PSSH=$(command -v parallel-ssh || command -v pssh) if [ "$PSSH" = "" ] ; then echo "Parallel SSH (pssh) is required for this script to work." exit 4 fi # Hosts' list file HOSTS="$HOME/.config/cluster/$1.lst" shift echo "Using file '$HOSTS' as hosts' list." # Login if [ "$LOGIN" != "" ] ; then echo "Login: $LOGIN" LOGIN="-l $LOGIN" fi echo "Command:" "$@" exec $PSSH --print $LOGIN -h "$HOSTS" -- "$@"