diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2022-06-21 11:31:21 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2022-06-21 17:21:59 -0700 |
commit | 404afc1f834054bb6a2d8687558d3de804504aed (patch) | |
tree | 28417cbeee2da91586da7d41e7014804d888bd27 /bin/ssh-and-tmux | |
parent | e3219c51e60846b795d294860597250236f16a9f (diff) | |
download | dotfiles-404afc1f834054bb6a2d8687558d3de804504aed.tar.gz |
ssh-and-tmux: add mosh support & tidy up the script
Diffstat (limited to 'bin/ssh-and-tmux')
-rwxr-xr-x | bin/ssh-and-tmux | 81 |
1 files changed, 60 insertions, 21 deletions
diff --git a/bin/ssh-and-tmux b/bin/ssh-and-tmux index 6f1d482e..1aca3efc 100755 --- a/bin/ssh-and-tmux +++ b/bin/ssh-and-tmux @@ -1,18 +1,41 @@ #!/bin/bash -# when mosh isn't installed on the server, but tmux is, use this to get a -# fairly persistent set of remote shell sessions. I used to use autossh here, -# but that means surprise dialogs asking to authorise use of an SSH key; -# instead, just hit <Ret> to restart a window's connection when it's -# convenient +# Copyright (C) 2020-2022 Sean Whitton # -# (The reason that I get prompted is because I have a short -# ServerAliveInterval and low ServerAliveCountMax, but that's desirable, -# because unresponsive SSH sessions are a massive pain) +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or (at +# your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. -getopt=$(getopt -s bash -o "" -l 'container-cmd:,container-name:' -n ssh-and-tmux -- "$@") -eval "set - $getopt" + +# Launcher for connections to remote persistent shell sessions, via mosh or a +# mechanism for manual, but convenient, user-triggered reconnects. +# +# Advantage of the latter over autossh is avoidance of surprise dialogs asking +# to authorise the use of SSH keys. And thus works better than autossh with +# short ServerAliveInterval and low ServerAliveCountMax ssh client config, +# which are desirable for interactive sessions. + + + +# Space-separated list of hosts I want to mosh to. Be conservative: it's not +# just that mosh is available on that machine and it's not a highly +# security-sensitive machine, but also that, e.g., the host reboots less often +# than the workstation that'll be connecting, to avoid zombie clients. +mosh_hosts="athena.silentflame.com" + +getopt=$(getopt -s bash -o "" \ + -l 'container-cmd:,container-name:' -n ssh-and-tmux -- "$@") +eval "set - $getopt" while true; do case "$1" in '--container-cmd') container_cmd=$2; shift 2; continue ;; @@ -21,26 +44,42 @@ while true; do '--') shift; break ;; esac done - host="$1" session="${2:-default}" +if printf '%s\0' "${mosh_hosts[@]}" \ + | grep -Fxqz "$host"; then use_mosh=true; else use_mosh=false; fi + # Go via a login shell such that if there is no root tmux daemon yet, it ends # up with a PATH as though the first tmux session were launched from an # interactive shell. This affects launching programs directly from tmux # (e.g. my C-\ e binding) for *all* sessions. -cmd="\$SHELL -l -c \"tmux new-session -A -c \$HOME -s $session\"" +# +# mosh does execvp directly so for simplicity we assume remote SHELL is bash. +cmd="tmux new-session -A -c \$HOME -s $session" +if $use_mosh; then + cmd=(bash -l -c "$cmd") +else + cmd=(\$SHELL -l -c "\"$cmd\"") +fi + if [ -n "$container_name" ]; then display_host="$container_name" - cmd="$(printf "$container_cmd" "$container_name") -- $cmd" + cmd=($(printf "$container_cmd" "$container_name") -- "${cmd[@]}") else display_host="$host" fi -echo -ne "\033]0;tmux $session on $display_host\007" -prompt="Press any key to reconnect to tmux session [1m$session[0m on [1m$display_host[0m; d/C-d/C-c to give up .." -while true; do - ssh -t "$host" "$cmd" - clear - read -n 1 -r -s -p "$prompt" ch - [ "$ch" = $'\04' -o "$ch" = d ] && echo && exit -done +if $use_mosh; then + mosh "$host" -- "${cmd[@]}" +else + echo -ne "\033]0;tmux $session on $display_host\007" + prompt="Press any key to reconnect to tmux session [1m$session[0m on " + prompt+="[1m$display_host[0m; d/C-d/C-c to give up .." + + while true; do + ssh -t "$host" "${cmd[*]}" + clear + read -n 1 -r -s -p "$prompt" ch + [ "$ch" = $'\04' -o "$ch" = d ] && echo && exit + done +fi |