Skip to content

Instantly share code, notes, and snippets.

@cypres
Last active December 13, 2015 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cypres/6e95d9d83e134afce036 to your computer and use it in GitHub Desktop.
Save cypres/6e95d9d83e134afce036 to your computer and use it in GitHub Desktop.
Workaround mosh wrapper
#!/usr/bin/env python3
import subprocess
import argparse
import re
import os
parser = argparse.ArgumentParser(description='Mosh connection tool.')
parser.add_argument('target', metavar='target', type=str, help='the SSH target')
parser.add_argument('--port', type=int, default=22, help='the SSH port')
parser.add_argument('--new', help='Open a new session', action='store_true')
args = parser.parse_args()
n = ' new -s' if args.new else ''
res = subprocess.run(['ssh', args.target, '-p', str(args.port), '-n', '-tt',
'--', 'LANG=en_US.UTF-8 mosh-server' + n],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
re_mosh = re.compile(b'^MOSH CONNECT (\d+) (.+)\\r?$', re.MULTILINE)
re_conn = re.compile(b'^Connection to ([0-9\.]+) closed\.\\r?$', re.MULTILINE)
mosh = re_mosh.search(res.stdout)
conn = re_conn.search(res.stderr)
if mosh and conn:
e = dict(os.environ, MOSH_KEY=mosh.group(2).strip(b'\r\n'))
ip = conn.group(1).decode('ascii')
port = mosh.group(1).decode('ascii')
os.execlpe('mosh-client', 'mosh-client', ip, port, e)
else:
print('Failed to get mosh connection info')
print(res.stdout.decode('utf-8'))
print(res.stderr.decode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment