Automatically Upgrade SSH Connections to Mosh when Available

A quick and dirty trick on how to automatically upgrade SSH connections to Mosh when available.

Automatically Upgrade SSH Connections to Mosh when Available

If you’re regularly working with SSH and Mosh connections, you’ve probably encountered this issue as well: You’re staring into the void that is your terminal, trying to remember whether the next machine you’ll be establishing a connection to supports Mosh or not. Ultimately you might just give it a try by typing mosh instead of ssh and hope for the best.

Let me tell you that there’s a quick and dirty solution to this – at least if you’ve been carefully maintaining your ~/.ssh/config as you should.

I’ve already briefly described my hack in a GitHub issue reply, but I thought it might make sense to cover it on this site as well, as many of you command line connoisseurs reading my journal might find value in this.

For this hack to work, I extended my .zshrc with the following function:

function ssh {
  if [ "$2" = "" ]
  then
    conn="$1"
    sshhost=$(printf "%s" "${conn}" | cut -d '@' -f2)
    if rg -U -i "^#.*Features:.*mosh.*\nHost ${sshhost}" "${HOME}/.ssh/config" > /dev/null
    then
      printf "connecting with mosh ...\n"
      command mosh ${conn}
    else
      printf "connecting with ssh ...\n"
      command ssh ${conn}
    fi
  else
    printf "connecting with ssh ...\n"
    command ssh $@
  fi
}

This function will take precedent over the ssh binary and will do a quick check of the aforementioned ~/.ssh/config file before deciding what to do. To perform the check it uses ripgrep. In particular, it will check for a comment above any Host configuration that says # Features: mosh. A ~/.ssh/config might for example look like this:

ServerAliveInterval 60
Host *
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519

# Features: mosh
Host host1
  HostName host1.domain.com
  Port 9876

Host host2
  HostName host2.domain.com
  Port 8765

In this configuration, host1 contains the mosh feature header. When the ssh function encounters this header it will go ahead and execute mosh instead of ssh to connect to the machine. In all other cases it will simply run ssh with the given parameters.

Obviously this hack has its limitations (especially in regard of command line arguments), but for straightforward connections using the Host configurations in ~/.ssh/config it works perfectly fine. As long as you keep the configuration properly maintained you might never have to pause and think on whether you can use mosh or not, because this will do it automatically for you.

As mentioned in the GitHub issue:

Right now the function only checks for a mosh connection when its called with a single argument (e.g. ssh user@host1), in order to avoid trying mosh for things like ssh -L 1234:localhost:2345 user@host1. This could be refined to handle more cases in the future though.

PS: The Features header can be used for other scripts in a similar fashion, simply by adjusting the rg check, e.g.

  ...
  if rg -U -i "^#.*Features:.*xyz.*\nHost ${sshhost}" "${HOME}/.ssh/config" ... 
  ...

Enjoyed this? Support me via Monero, Bitcoin, Lightning, or Ethereum!  More info.