Leveraging Synergy across two platforms…

Sounds like a line from a certain “pointy-haired boss,” doesn’t it?

I use a MacBook Pro running OS X Leopard as my main system at the office. I have a secondary display connected to it and I use the built-in “Spaces” feature. My secondary system, a desktop, is running Ubuntu 8.10 workstation. For several months now, I have had two keyboards and mice to deal with. There had to be a way to share one keyboard and mouse between the two systems.

Enter the utility, Synergy.

From the Synergy website:

Synergy lets you easily share a single mouse and keyboard between multiple computers with different operating systems, each with its own display, without special hardware. It’s intended for users with multiple computers on their desk since each system uses its own monitor(s).

Redirecting the mouse and keyboard is as simple as moving the mouse off the edge of your screen. Synergy also merges the clipboards of all the systems into one, allowing cut-and-paste between systems. Furthermore, it synchronizes screen savers so they all start and stop together and, if screen locking is enabled, only one screen requires a password to unlock them all.”

This is exactly what the doctor ordered. What I found, however, is that it is not a simple as clicking “Next -> Next -> Finish.” To get this working on Linux and OS X, a person has to get their hands a bit dirty.

In the most basic sense, Synergy works by running a server program on the system that has the keyboard and mouse that you want to use, and a client program on the computer that you want to control. On ‘nix based systems, which includes the Mac, this is all done through a terminal interface. There is no slick GUI. (I did notice that there are some projects out there that are working on a GUI, but none looked mature enough at the time of this writing.)

Using a MacBook Pro laptop as the server throws a wrench in the works as well. I need to grab the laptop and take it with me to meetings and when I leave at night. I don’t want to leave the Synergy programs running when I’m not at my desk. If I want to start and stop the program on both systems every time the Mac comes and goes from my desk, it is a manual process that has to be done from the keyboard and mouse physically connected to each system.

Big Pain. I want to click on something on the Mac that will start or stop the programs running on both the Mac and the Linux workstation. I did find a way to make this work. I won’t go into detail on the basic set up of Synergy on each platform, as that is covered on their website. I will outline what I did to make this work the way I wanted it to on both the Mac and the Linux system.

The first thing to take note of is a bug in the way the Synergy server runs on OS X. Usually, when you execute the synergys program, it launches and runs as a background process. On OS X Leopard, you get an error and the program stops. You can, launch it with the -f option that forces the process to continue running in the foreground. The bad thing about this is that you need to keep the terminal window open while synergy is running.

To get Synergy to run as a background process, I used the screen command to execute it. This command will let you detach a process from the terminal. That means, you can kick off synergys with the -f option and it will become a background process. More on this in a bit.

The next big hurdle is remotely executing the start and stop scripts on the Linux system. By using some SSH tools, I added a public encryption key to my profile on the Linux system. This prevents me from keying a password when I make an SSH connection to the Linux system. Having already created the scripts on the Linux workstation, all I had to do was use an SSH statement in my shell script to execute the remote script and start the Synergy client.

First, run ssh-keygen on the Mac to generate your key pair. There will be a public and a private key. After that, the public key needs to be distributed to the remote system. OpenSSH includes a shell script called ssh-copy-id that will do this for you. Unfortunately, OS X doesn’t have that script, so you can use this one instead:

—–
#!/bin/sh

KEY=”$HOME/.ssh/id_dsa.pub”

if [ ! -f ~/.ssh/id_dsa.pub ];then
echo “private key not found at $KEY”
echo “* please create it with “ssh-keygen -t dsa” *”
echo “* to login to the remote host without a password, don’t give the key you create with ssh-keygen a password! *”
exit
fi

if [ -z $1 ];then
echo “Please specify user@host.tld as the first switch to this script”
exit
fi

echo “Putting your key on $1… “

KEYCODE=`cat $KEY`
ssh -q $1 “mkdir ~/.ssh 2>/dev/null; chmod 700 ~/.ssh; echo “$KEYCODE” >> ~/.ssh/authorized_keys; chmod 644 ~/.ssh/authorized_keys”

echo “done!”
—–

I found that script here.

Save it as ssh-copy-id and make it executable. Then execute it like this:

./ssh-copy-id userid@servername

You will be prompted for your password this time, so that the key can be copied. Now test this by issuing the following command:

ssh userid@servername

If you weren’t prompted for your password, you win!

I decided that the best way to handle all this process starting and stopping is with a shell script. I already have an Automator script that connects my Mac to some network shares and sets the volume to an office-friendly level. In addition, I have an Automator script that reverses that process when I leave at night. By creating shell scripts for Synergy and adding them to my existing Automator scripts, things will be much easier.

Here is my script with server and user names removed:

—–
#!/bin/sh
# start-synergy
# Script to start Synergy on remote linux system and local system

screen -d -m /Applications/synergy-1.3.1/synergys -f –config /Applications/synergy-1.3.1/synergy.conf -f

ssh userid@servername /home/userid/scripts/startsynergy
—–

The first line of that script launches the local Synergy server process. The second line launches the remote script through SSH.

There is the script I use to stop Synergy:

—–
#!/bin/sh
# stop-synergy
# Script to stop Synergy on ubuntu01blea and local system

killall synergys

ssh userid@servername /home/userid/scripts/stopsynergy
—–

You can test these scripts in a terminal window.

Now, to add this shell script to Automator, select “Utilities” and then “Run Shell Script”.

Type the full path to your script in the properties of that object.

Add any other items you want to your Automator script and save it.

That’s it! You’re done!

Leave a Reply