Skip to content

Using Mosh on the department systems

Zachary Espiritu edited this page Apr 9, 2020 · 4 revisions

What's mosh?

Mosh is a replacement for ssh(1). It's more robust and responsive, especially over Wi-Fi, cellular, and long-distance links as it supports intermittent connectivity and smoother client-side interaction.

Here's some example use-cases for Mosh, taken from their home page:

  • Mosh automatically roams as you move between Internet connections. Use Wi-Fi on the train, Ethernet in a hotel, and LTE on a beach: you'll stay logged in.
  • With Mosh, you can put your laptop to sleep and wake it up later, keeping your connection intact. If your Internet connection drops, Mosh will warn you — but the connection resumes when network service comes back.
  • ...and more!

How to Use mosh

General (Platform-Agnostic Instructions)

  1. Install mosh.

  2. Set up OpenVPN on your local machine using the Brown CS instructions.

  3. Connect to either the <username>_browncs.ovpn or <username>_browncs_gateall.ovpn endpoints. Either one works for the purposes of using mosh; *_browncs.ovpn will only route your cs.brown.edu traffic through the VPN as opposed to *_browncs_gateall.ovpn which will route all traffic through the VPN.

  4. Connect to mosh using the following command, where <ssh-address> is in the form USERNAME@ssh.cs.brown.edu (the public key endpoint, pk-ssh.cs.brown.edu, also works):

    mosh --experimental-remote-ip=remote <ssh-address>

    The --experimental-remote-ip=remote flag is needed as the ssh.cs.brown.edu server is an SSH forwarder, so you'll end up on a machine that will have a different IP address from that of the ssh endpoint. mosh needs this flag in order to handle this forwarding mechanism.

  5. You're all set!

Extra Things To Do with mosh

This section contains optional instructions for using mosh for various operating systems. They are not required to use Mosh.

macOS

If you're using MacOS and are using Tunnelblick to connect to the OpenVPN server (as recommended by the Brown CS setup page), you can control Tunnelblick from the command-line using AppleScript. This can be helpful as you can then set up an alias that automatically connects to the Brown CS VPN when you try to run mosh so you don't have to remember to turn it on manually beforehand.

  1. Copy the two AppleScript files below to a location on your computer. brown-tunnelblick-connect.scpt connects your computer to the browncs OpenVPN endpoint, and brown-tunnelblick-disconnect.scpt disconnects your computer from the VPN. Each script can run from the command-line with the following:

    osascript <path-to-script>

    For the purposes of this tutorial, I'll assume these scripts are stored in the ~/scripts directory.

  2. Write some aliases in your shell configuration file (ex: .bashrc) to automatically start Tunnelblick when you want to run mosh:

    alias vpn-connect="osascript ~/scripts/brown-tunnelblick-connect.scpt"
    alias vpn-disconnect="osascript ~/scripts/brown-tunnelblick-disconnect.scpt"
    alias brown-mosh="vpn-connect && mosh --experimental-remote-ip=remote <ssh-address> && vpn-disconnect"

    Running the brown-mosh command will then start the VPN, run mosh, then disconnect from the VPN when mosh exits.

  3. That's it!

brown-tunnelblick-connect.scpt

This script will attempt to connect to the <username>_browncs endpoint if your computer is not currently connected to the VPN:

set vpnConfig to "<username>_browncs"
tell application "/Applications/Tunnelblick.app"
  set vpnState to (get state of first configuration where name = vpnConfig)
  if vpnState = "Exiting" then  -- "Exiting" means that it's not currently connected
    connect vpnConfig
  end if
end tell

brown-tunnelblick-disconnect.scpt

This script will disconnect your computer from the <username>_browncs endpoint if you're currently connected to it:

tell application "/Applications/Tunnelblick.app"
  disconnect "<username>_browncs"
end tell