Skip to content

Instantly share code, notes, and snippets.

@TBonnin
Created July 18, 2014 19:00
Show Gist options
  • Save TBonnin/8b730bee184614e87936 to your computer and use it in GitHub Desktop.
Save TBonnin/8b730bee184614e87936 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
THIS_BASENAME=$(basename "$0")
TMUX_SESSION_NAME=$THIS_BASENAME
HOSTS=""
USER=""
CERTIFICATE=""
FILENAME=""
SSH_ARGS=""
# Config-file in home directory
CONFIG_FILENAME=".tmux-cssh"
HOME_CONFIG_FILENAME="$(echo ~)/${CONFIG_FILENAME}"
CONFIG_SETTING_NAME=""
# Outputs script syntax and help
syntax() {
echo "Syntax: ${THIS_BASENAME} [-h|-u [user]|-c [path to certificate]|-sc [ssh-server-connect-string]|-sa [additional ssh arguments]|-ts [session-name]|-ns|-q|-f [filename]|-cs [name of config-parameters to use]]"
echo
echo "-h | --help This help."
echo
echo "-u | --user User to use."
echo
echo "-c | --certificate Path to ssh-certificate to use."
echo
echo "-sc | --ssh-server-connect-string SSH-connection-string, multiple."
echo
echo "-sa | --ssh_args SSH connection arguments, used on every session."
echo
echo "-ts | --tmux-session-name Alternative tmux-session-name, default: ${THIS_BASENAME}"
echo
echo "-ns | --new-session Initializes a new session, like -ts [name]."
echo
echo "-q | --quiet Quiet-mode."
echo
echo "-f | --filename Filename of textfile to get -sc connection-strings from, line separated."
echo
echo "-cs | --config-setting Name of config-settings which should be get from config-file '${HOME_CONFIG_FILENAME}'. Which can be a grep-regular expression to find the name(s)."
echo
echo "* Other arguments will be interpreted as '-sc'."
echo
echo "* For a maybe newer version of ${THIS_BASENAME} take a look on https://github.com/dennishafemann/tmux-cssh"
echo
}
# Output given parameters if not in quiet-mode
output() {
if [ "${QUIET}" != "true" ]; then
echo "$@"
fi
}
# Analyse given parameter into environment
analyseParameters() {
# Walk through parameters
for param in "$@"; do
case "$1" in
-h|--help) syntax; exit;;
-u|--user) USER="$2"; shift;;
-c|--certificate) CERTIFICATE="$2"; shift;;
-sc|--ssh-server-connect-string) HOSTS="${HOSTS} $2"; shift;;
-sa|--ssh_args) SSH_ARGS="$SSH_ARGS $2"; shift;;
-ts|--tmux-session-name) TMUX_SESSION_NAME="$2"; shift;;
-ns|--new-session) TMUX_SESSION_NAME="${TMUX_SESSION_NAME}_$(date +%s)";;
-q|-quiet) QUIET="true";;
-f|--filename) FILENAME="$FILENAME $2"; shift;;
-cs|--config-setting) CONFIG_SETTING_NAME="$2"; shift;;
*) HOSTS="${HOSTS} $1";;
esac
shift
done
}
# Check if tmux is available
if [ "$(which tmux)" == "" ]; then
echo "${THIS_BASENAME}"
echo
echo "TMUX is not avaiable."
echo
exit;
fi
# Check main parameters
analyseParameters $@
# Check if a config-settings name is given
if [ "${CONFIG_SETTING_NAME}" != "" ]; then
# If home-config-file available
if [ -f "${HOME_CONFIG_FILENAME}" ]; then
# Read every line from config-settings file
while read configSettingsRow; do
# Seperate Name and parameters
configSettingsName=$(echo ${configSettingsRow} | cut -d ":" -f 1)
configSettingsParameters=$(echo ${configSettingsRow} | cut -d ":" -f 2)
# Match name against given settings-name
matched=$(echo ${configSettingsName} | grep "^${CONFIG_SETTING_NAME}$")
# Machted ?
if [ "${matched}" != "" ]; then
output "* Matched config-settings '${matched}', analysing parameters."
# Check config parameters
analyseParameters ${configSettingsParameters}
fi
done < "${HOME_CONFIG_FILENAME}"
else
output "No config-settings-file '${HOME_CONFIG_FILENAME}' available."
fi
fi
# Check if filenames with connection-strings are given
if [ "$FILENAME" != "" ]; then
output "* Reading file '${FILENAME}' with host-connection-string."
# Walk through all given filenames
for filename in $FILENAME; do
# Walk through all available connection strings
while read connHost; do
output "* Adding host '${connectionString}'"
# Add connection string to currently set hosts
HOSTS="$HOSTS $connHost"
done < "${filename}"
done
fi
# Check if tmux-session is available
if (( $(tmux ls 2> /dev/null | grep "${TMUX_SESSION_NAME}" | wc -l) > 0 )) ; then
tmux attach -t "${TMUX_SESSION_NAME}"
exit
fi
# Hosts available ?
if [ "${HOSTS}" == "" ]; then
output "* Hosts not given."
syntax
exit
fi
initTmuxCall="true"
# Walk through hosts
for host in ${HOSTS[@]}; do
connectString="${host}"
# Add user-part
if [ "${USER}" != "" ]; then
connectString="${USER}@${connectString}"
fi
# Add certificate-part
if [ "${CERTIFICATE}" != "" ]; then
connectString="-i ${CERTIFICATE} ${connectString}"
fi
# Add ssh_args-part
if [ "${SSH_ARGS}" != "" ]; then
connectString="${SSH_ARGS} ${connectString}"
fi
# Finalize connect-string
connectString="ssh ${connectString}"
# Output
output "* Connecting '${connectString}'"
# First Call, inits the tmux-session
if [ "${initTmuxCall}" == "true" ]; then
tmux new-session -d -s "${TMUX_SESSION_NAME}" "${connectString}"
initTmuxCall="false"
# Attach zu tmux-session
else
tmux split-window -t "${TMUX_SESSION_NAME}" "${connectString}"
tmux select-layout -t "${TMUX_SESSION_NAME}" tiled
fi
done
# Set pane synchronisation
tmux set-window-option -t "${TMUX_SESSION_NAME}" synchronize-panes on
# Attach to tmux session
tmux attach-session -t "${TMUX_SESSION_NAME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment