[ssh_tools] tabssh: modernize

This commit is contained in:
Matteo Cypriani 2019-10-21 18:19:21 +02:00
parent 665ca65cd2
commit 81988f3c00
1 changed files with 40 additions and 22 deletions

View File

@ -12,39 +12,57 @@
# 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_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
# Parse the optional arguments
if [ "$1" = "-l" ] ; then
LOGIN="$2"
shift
shift
else # Option unknown
print_usage
exit 1
fi
# Only -l is a valid option
[ "$1" = "-l" ] || bad_usage "Option '$1' unknown."
LOGIN="$2"
shift
shift
# ... or only 1
# ... or only 1 argument
elif [ $# -ne 1 ] ; then
print_usage
exit 3
bad_usage "Wrong number of arguments."
fi
# Hosts' list file
if [ -z "$XDG_CONFIG_HOME" ] ; then
XDG_CONFIG_HOME="$HOME/.config"
fi
HOSTS_FILE="$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_FILE' as hosts' list."
if [ ! -f "$HOSTS_FILE" ] ; then
echo "The hosts' list file doesn't exist or is not a regular file."
exit 2
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
@ -58,4 +76,4 @@ while read -r HOST ; do
SSH="ssh ${LOGIN}${HOST}"
# shellcheck disable=SC2086
screen -t "$SSH" $SSH
done <"$HOSTS_FILE"
done <"$HOSTS"