scripts/cluster/cluster-ssh.sh

68 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
#
# cluster-ssh.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.
#
# Open an SSH interactive shell on a list of remote hosts, opening one
# GNU Screen tab per host.
print_usage()
{
echo "Usage: $0 [-l login] <hosts_list>"
}
# 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 only one argument left?
if [ $# -ne 1 ] ; then
print_usage
exit 3
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="${LOGIN}@"
fi
# Create the screen tabs
for HOST in $(cat "$HOSTS") ; do
SSH="ssh ${LOGIN}${HOST}"
screen -t "$SSH" $SSH
done