Skip to content

Instantly share code, notes, and snippets.

@mortie
Created August 5, 2022 21:24
Show Gist options
  • Save mortie/f8bf0c7798c8e33c41cd9e08c864f73b to your computer and use it in GitHub Desktop.
Save mortie/f8bf0c7798c8e33c41cd9e08c864f73b to your computer and use it in GitHub Desktop.
sst: Reliable SSH connections using tmux
#!/bin/sh
# SST: SSH, but reliable, using tmux.
# When you lose connection, the connection will be re-tried, and you will get back where you
# left off.
# An aggressive heartbeat setting is used to detect network issues much more reliably than
# SSH's default settings would.
#
# Usage:
# sst <host>: Create a temporary tmux session, which will be killed when it hasn't
# been used in a while
# sst <host> <session>: Connect to an existing tmux session
if [ -z "$1" ]; then
echo "Usage: $0 <server> [tmux session]"
fi
server="$1"
if [ -n "$2" ]; then
tmux="$2"
temporary=0
else
id="sstemp-$RANDOM"
tmux="$id"
temporary=1
fi
if [ "$temporary" = 1 ]; then
tempfile="/tmp/$USER-$id"
cmd="([ -n \"\$BASH_VERSION\" ] && shopt -s huponexit); "
cmd="${cmd}([ ! -e $tempfile ] && nohup >$tempfile.nohup 2>&1 sh -c \"while :; do sleep 100; if ! [ -e $tempfile ]; then tmux kill-session -t $tmux; break; fi; rm -f $tempfile; done\") & "
cmd="${cmd}if [ -e $tempfile ]; then NEWSESSION=0; else NEWSESSION=1; fi; "
cmd="${cmd}(while :; do touch $tempfile; sleep 10; done) & "
cmd="${cmd}if [ \$NEWSESSION = 0 ]; then tmux attach -t $tmux; else tmux new -s $tmux; fi"
else
cmd="tmux attach -t $tmux"
fi
while :; do
ssh -t -o 'ServerAliveInterval 2' -o 'ServerAliveCountMax 2' "$server" "$cmd" && break
sleep 2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment