Skip to content

Instantly share code, notes, and snippets.

@marcusramberg
Forked from tribut/mosh_pc.sh
Last active August 29, 2015 14:10
Show Gist options
  • Save marcusramberg/01b68abe4a52d76ccf42 to your computer and use it in GitHub Desktop.
Save marcusramberg/01b68abe4a52d76ccf42 to your computer and use it in GitHub Desktop.
#!/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