A thing that has been driving me up the wall about my synergy setup at work is this. The main server is the Windows laptop that I use, which is fine when it’s docked, and when it’s mobile the server is disconnected from the clients (counter-intuitive I know but bear with me). The server only runs on the wired ethernet so the client doesn’t pick it up over wireless.

All well and good, when docked I have Windows dual screen with Ubuntu joy over on the the third screen.

Now, the only small problem is everything is on DHCP… and every so often the addresses change. Which is annoying. It means reconfiguration by hand on the Ubuntu box, after first running ipconfig on the windows box (by the way, anyone want to point me at an app that pops up the IP address in a tooltip over the network icon on the tray ? Make this a whole lot easier…).

The solution ? Automate (finally). Involving the magic of nmblookup (DHCP client on the laptop does not register itself as a DNS entry, so you find the server via WINS, which Ubuntu needs help with).

The script (see the cut) attempts to figure out if the synergy client is running or if it has died, if it has died, it attempts to restart, after first finding the server. The client should die when the connection is killed (simulate this by clicking on the “Force Reconnection” option in the Windows Synergy server context menu). So essentially, undock the laptop, and the connection is killed. If the laptop is switched off or not connected, the ip address is not available and the script with try again later. The Script itself is run via crontab. The ip address is extracted from nmblookup by using gawk (see after cut).

Awk Script

   1: #!/usr/bin/awk --posix -f


   3: #

   4: # Awk script to return the IP of my laptop to

   5: # find it back when it wanders you need the posix

   6: # flag to allow the '{n,m}' operator

   7: #


   9: # IP address

  10: /^[0-9]{1,3}(.[0-9]{1,3}){3}/ { 

  11:     print $1

  12: }

Shell Script

   1: #!/bin/sh


   3: #

   4: # Sync the synergy clients

   5: #


   7: timestamp=`date '+%Y-%m-%d %H:%M:%S '`

   8: pid=`ps -U LUSER -u LUSER ax | awk '/synergyc/ && !/awk/ { print $1}'`

   9: server=`nmblookup LAPTOP| gawk --posix -f ~/bin/synergy.awk`

  10: opts="--no-restart --debug WARNING --daemon"

  11: pidfile=~LUSER/.synergy.pid


  13: if [ ! -e $pidfile ]; then

  14:     if [ -z $pid ]; then

  15:     echo $timestamp "Synergy not running - starting up"

  16:     else

  17:     echo $timestamp "Synergy running - writing pidfile"

  18:     echo $pid > $pidfile

  19:     exit

  20:     fi

  21: else

  22:     currpid=`cat $pidfile`

  23:     synergyproc=`ps -p $currpid | awk '/synergyc/ { print $1 }'`

  24:     if [ -z $synergyproc ]; then

  25:     if [ -z $pid ]; then

  26:         echo $timestamp "Synergy died - starting up"

  27:     else 

  28:         echo $timestamp "Synergy running - replacing pidfile"

  29:         echo $pid > $pidfile

  30:         exit

  31:     fi

  32:     else

  33:     echo $timestamp "Synergy running fine"

  34:     exit

  35:     fi

  36: fi


  38: #

  39: # We get to here, it's dead and needs a restart.

  40: #


  42: if [ -z $server ]; then

  43:     echo $timestamp "Server dead - not starting"

  44:     exit

  45: else

  46:     /usr/bin/synergyc $opts $server

  47:     echo $? > $pidfile

  48:     exit

  49: fi


If there are two IP addresses for the same box, the behaviour is a little undefined, you’d need to modify the gawk script to fix this.