Last active
November 22, 2017 19:16
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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