Skip to content

Instantly share code, notes, and snippets.

@cybic
Last active November 22, 2017 19:16
Show Gist options
  • Save cybic/2db49fe1cd3fe1e9c097 to your computer and use it in GitHub Desktop.
Save cybic/2db49fe1cd3fe1e9c097 to your computer and use it in GitHub Desktop.
Connect to a server using AutoSSH and attach to or create a tmux session
#!/bin/bash
#
# AutoSSH to host and create or connect to tmux session
#
# Author: Oystein Steimler <[email protected]>
if [[ -z "$1" || -z "$2" ]]
then
echo -e "Missing parameter.\n\nUsage:\n\tremux <host> <session> [ssh-opts]\n\nExample:\n\tremux [email protected] session -4\n";
exit 1
fi
# Check for autossh
if [[ ! -z $(which autossh) ]]
then
# Autossh with quick reconnect
SSH="`which autossh` -M0 -o ServerAliveInterval=10 -o ServerAliveCountMax=2 -o BatchMode=yes"
else
# Plain ssh if no autossh is found
SSH=$(which ssh)
fi
#if tmux ls -F '#{session_name}' | grep ^$2
$SSH -t $3 $1 "
if tmux ls | grep ^$2
then
tmux att -t $2
else
tmux new -s $2
fi
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment