Skip to content

Instantly share code, notes, and snippets.

@devynspencer
Last active September 29, 2015 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devynspencer/22086a990dd7bc12b2e7 to your computer and use it in GitHub Desktop.
Save devynspencer/22086a990dd7bc12b2e7 to your computer and use it in GitHub Desktop.
Open multiple ssh connections to a host using individual tmux panes, but within a single tmux window for sanity, finger-health, and glory!
#!/bin/bash
#
# description: creates a new tmux window and multiple ssh sessions based on given parameters.
# syntax: ssh_group [TARGET] [CONNECTIONS]
# example: open 4 ssh connections to host `unicorn.rainbow.pvt`
# ssh_group unicorn.rainbow.pvt 4
# note: use from within an existing tmux session
if [ -z "$1" ]; then
if [ -z "$SSH_CONCENTRATOR" ]; then
echo "No default value for SSH_CONCENTRATOR. Exiting."
echo "Set value using export SSH_CONCENTRATOR='hostname.domain'"
exit
else
echo "No host specified. Using $SSH_CONCENTRATOR instead."
TARGET=$SSH_CONCENTRATOR
fi
else
TARGET=$1
fi
if [ -z "$2" ]; then
echo "No. of connections not specified. Using 4 connections instead."
CONNECTIONS=4
else
CONNECTIONS=$2
fi
starttmux() {
# pass shell arguments to tmux
local target=( $TARGET )
local connections=( $CONNECTIONS )
# create tmux window & ssh to host
tmux new-window "ssh -q ${target}"
# create additional connections
for ((n=1; n<${connections}; n++)); do
tmux split-window -h "ssh -q ${target}"
done
# reconfigure layout to fit window
tmux select-layout tiled > /dev/null
# set input to first pane
tmux select-pane -t 0
}
starttmux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment