how to run synergy in a ssh tunnel from a unix workstation to a mac laptop

this shell script can be used for both the server and the client of a synergy setup. (well, it assumes that only two machines are involved.) it can start and stop synergy, as well as create and destroy the ssh tunnel. for security, the server only listens on the localhost interface, and the client connects via a ssh tunnel.

extra ssh options are used to overcome some past problems, but may soon be unecessary:

  • 4: ipv4 only. ipv6 isn’t yet common, and some os libraries choke when they can’t find an ipv6 address for an interface.

  • 2: protocol 2 only. some ssh implementations allow the less secure version 1 protocol.

the script can also run the client or server in the foreground for debugging.

it’s usually easier to keep the configuration in ~/.synergy.conf so root priv’s aren’t needed to edit it. here’s a simple sample that will work on both ends (also included in the script header comments).

# wox = unix workstation
# mox = mac laptop

section: screens
    wox:
    mox:
      # make mac caps lock key a toggle
      halfDuplexCapsLock = true
end

section: links
    wox:
      left = mox
    mox:
      right  = wox
end

= never works, but i keep trying...
section: options
     screenSaverSync = true
end

# map fqdn to short names, if needed
# try to avoid, if possible. life is just easier. :)
# if short names used, probably need to do synergyc -n
# see man(1) synergyc
#
#section: aliases
#  wox.example.com:
#    wox
#  mox.example.com:
#    mox
#end

synergy script:

#!/bin/sh
# synergy - create reverse tunnel thru grox
# author: dave w capella  <http://grox.net/mailme>
# date  : Sun Sep 21 03:19:32 PDT 2008
# notes :
#  server listens only on localhost for improved security
#  client connects via ssh tunnel for improved security
#
# use with ~/.synergy.conf on server and client. simple example:
#
# ==========================================================
#  # wox = unix workstation
#  # mox = mac laptop
#
#  section: screens
#    wox:
#    mox:
#      # make mac caps lock key a toggle
#      halfDuplexCapsLock = true
#  end
#
#  section: links
#    wox:
#      left = mox
#    mox:
#      right  = wox
#  end
#
#  # never works, but i keep trying...
#  section: options
#     screenSaverSync = true
#  end
#
#  # map fqdn to short names, if needed
#  # try to avoid, if possible. life is just easier. :)
#  # if short names used, probably need to do synergyc -n
#  # see man(1) synergyc
#  #
#  #section: aliases
#  #    wox.example.com:
#  #            wox
#  #    mox.example.com:
#  #            mox
#  #end
# ==========================================================
#
############################################################
PATH=/bin:/usr/bin:/usr/local/bin
[ `uname` = Darwin ] && PATH=$PATH:/opt/local/bin
_program=`basename $0`
_foreground=no
_server=wox

usage(){
  cat >&2 <<EOM

$_program - run synergy more securely as a client or a server

usage:
$_program -h | -k | [-f] -[s|c]

-h    print this help message and quit
-k    kill synergy and, if client, ssh tunnel
-f    runs synergy in the foreground.
-s    start the server
-c    start the client. creates ssh tunnel to server.

EOM
  exit 2
}


[ $# -eq 0 ] && usage
while [ $# -gt 0 ] ; do
  case "$1" in
    -f)
        _foreground=yes
        ;;
    -s) # server
        if [ $_foreground = yes ] ; then
          exec synergys -f -a 127.0.0.1 &
        else
          exec synergys -a 127.0.0.1 &
        fi
        ;;
    -c) # client
        ps axo pid,args | awk '
            BEGIN { err = 1 }
            $4 ~ /24800/{ err = 0 }
            END { exit(err) }
          ' || ssh -42faxNL 24800:127.0.0.1:24800 $_server
        if [ $_foreground = yes ] ; then
          exec synergyc -f localhost &
        else
          exec synergyc localhost &
        fi
        ;;
    -k)
        _pid=`ps axopid,comm | awk '$2 ~ /synergy/{print $1}'`
        if [ -n "$_pid" ] ; then
          kill $_pid
        else
          echo "synergy doesn't appear to be running."
        fi
        _pid=`ps axopid,comm | awk '$4 ~ /24800/{print $1}'`
        exit
        if [ -n "$_pid" ] ; then
          kill $_pid
        else
          echo "ssh tunnel doesn't appear to be up."
        fi
        ;;
     *)
        usage
        ;;
  esac
  shift
done

######################################################################
# eof: synergy             vim: set ts=2 sts=2 sw=2 tw=80 et:

7/2010