97 lines
2.1 KiB
Bash
Executable File
97 lines
2.1 KiB
Bash
Executable File
#!/bin/sh
|
|
#
|
|
# tabssh.sh, Copyright © 2013, 2019 Matteo Cypriani <mcy@lm7.fr>
|
|
# (Formerly named cluster-ssh.sh)
|
|
#
|
|
# 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.
|
|
|
|
#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] <hosts_list>"
|
|
}
|
|
|
|
bad_usage()
|
|
{
|
|
err "$@"
|
|
echo >&2
|
|
print_usage >&2
|
|
exit $EXIT_USAGE
|
|
}
|
|
|
|
err()
|
|
{
|
|
printf 'Error! ' >&2
|
|
printf '%s\n' "$@" >&2
|
|
}
|
|
|
|
LOGIN=
|
|
|
|
# We must have either 3 arguments...
|
|
if [ $# -eq 3 ] ; then
|
|
# Only -l is a valid option
|
|
[ "$1" = "-l" ] || bad_usage "Option '$1' unknown."
|
|
LOGIN="$2"
|
|
shift
|
|
shift
|
|
|
|
# ... or only 1 argument
|
|
elif [ $# -ne 1 ] ; then
|
|
bad_usage "Wrong number of arguments."
|
|
fi
|
|
|
|
# Check dependencies
|
|
TMUX_CMD=$(command -v tmux)
|
|
SCREEN_CMD=$(command -v screen)
|
|
if [ -n "$TMUX_CMD" ] ; then
|
|
echo "tmux is available, we'll be using it."
|
|
elif [ -n "$SCREEN_CMD" ] ; then
|
|
echo "tmux is not available, we'll be using GNU screen instead."
|
|
else
|
|
err "Either tmux or GNU screen is required by this script."
|
|
exit $EXIT_DEPENDENCY
|
|
fi
|
|
|
|
# Hosts list file
|
|
[ -z ${XDG_CONFIG_HOME+x} ] && XDG_CONFIG_HOME="$HOME/.config"
|
|
HOSTS_LIST_NAME="$1"
|
|
shift
|
|
HOSTS="$XDG_CONFIG_HOME/ssh_tools/${HOSTS_LIST_NAME}.lst"
|
|
echo "Using file '$HOSTS' as hosts list."
|
|
if [ ! -f "$HOSTS" ] ; then
|
|
err "The hosts list file doesn't exist or is not a regular file."
|
|
exit $EXIT_HOSTS_LIST
|
|
fi
|
|
|
|
# Login
|
|
if [ -n "$LOGIN" ] ; then
|
|
echo "Login: $LOGIN"
|
|
LOGIN="${LOGIN}@"
|
|
fi
|
|
|
|
# Create the screen tabs
|
|
while read -r HOST ; do
|
|
SSH="ssh ${LOGIN}${HOST}"
|
|
if [ -n "$TMUX_CMD" ] ; then
|
|
$TMUX_CMD new-window -d -n "$SSH" -- "$SSH"
|
|
else
|
|
# shellcheck disable=SC2086
|
|
$SCREEN_CMD -t "$SSH" $SSH
|
|
fi
|
|
done <"$HOSTS"
|