Skip to content

Instantly share code, notes, and snippets.

@tribut
Last active June 4, 2018 11:33
Show Gist options
  • Save tribut/5285883 to your computer and use it in GitHub Desktop.
Save tribut/5285883 to your computer and use it in GitHub Desktop.
wrapper for mosh to work with ssh's proxycommand directive. this only makes sense if the target machine is directly reachable from the internet using udp (but probably not via tcp). usage: mosh_pc.sh [host as mentioned in .ssh/config] [public ip of host]
#!/bin/sh
# ########################################################## #
# wrapper for mosh to work with ssh's proxycommand directive #
# this only makes sense if the machine is directly reachable #
# from the internet using udp. #
# ########################################################## #
THISSCRIPT="`basename \"$0\"`"
REMOTE="$1"
REMOTEIP="$2"
debug() {
echo "[$THISSCRIPT] $@" >&2
}
usage() {
debug "use me like this: $THISSCRIPT host [ip]"
}
# some default values
if [ -z "$REMOTEIP" ]; then
if [ -z "$REMOTE" ]; then
usage
exit 1
fi
# does the remote have a hostname listed in .ssh/config
REMOTEHOST="`grep -E -C1 \"^Host ([a-zA-Z0-9 ]+ )?$REMOTE( [a-zA-Z0-9 ]+)?$\" ~/.ssh/config | tail -n1 | sed -r 's/\W*Hostname\W+//'`"
if [ -z "$REMOTEHOST" ]; then REMOTEHOST="$REMOTE"; fi
# try to resolve hostname
REMOTEIP="`dig +short $REMOTEHOST`"
if [ -z "$REMOTEIP" ]; then
debug "could not resolve hostname $REMOTE"
exit 1
fi
fi
debug "starting mosh-server on remote server $REMOTE"
MOSHDATA="`ssh -t \"$REMOTE\" mosh-server new | grep '^MOSH' | tr -d \"\r\n\"`"
if [ -z "$MOSHDATA" ]; then
debug "mosh-server could not be started"
exit 1
fi
PORT="`echo -n \"$MOSHDATA\" | cut -s -d' ' -f3`"
MKEY="`echo -n \"$MOSHDATA\" | cut -s -d' ' -f4`"
if [ -z "$PORT" -o -z "$MKEY" ]; then
debug "got no parseable answer"
exit 1
fi
debug "starting local mosh-client to $REMOTEIP"
MOSH_KEY="$MKEY" exec mosh-client "$REMOTEIP" "$PORT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment