!/usr/bin/perl use warnings; use strict; use Data::Dumper; use Sys::Syslog; # Let's get some syslog action goin openlog('SYNERGY-LOCATION','', 'local0'); # Servers to connect to my $home_server = "192.168.0.50"; my $work_server = "10.240.220.50"; # SSIDs we care about my $home_ssid = "HOME_SSID"; my $work_ssid = "WORK_SSID"; # Synergy Options my $synergy_app = "/Applications/Synergy.app/Contents/MacOS/synergyc"; my $synergy_server_port = "24800"; my $screen_name = "LOCALHOSTNAME"; # Airport Utility my $airport_app = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I"; ############ # MAIN ############ my ($synergy_status, $synergy_location) = getSynergyStatus(); #print "Synergy Status: $synergy_status @ $synergy_location\n"; my $current_ssid = getSSID(); #print "Current SSID: $current_ssid\n"; # Are we at work? if ( $current_ssid =~ /$work_ssid/ ) { # Is synergy running? if ( $synergy_status ) { # Is synergy running for work? if ( $synergy_location =~ /$work_server/ ) { #print "We're good!\n"; syslog('notice', "SSID:$current_ssid and location $synergy_location, doing nothing..."); } # The assumption is, if it's running but not in the right place, kill # the current process and restart it with the right info else { syslog('notice', "restarting synergy:$synergy_status and connecting to $work_server"); controlSynergy("restart", $work_server, $synergy_status); } exit; } else { syslog('notice', "starting synergy and connecting to $work_server"); controlSynergy("start", $work_server); } } # Are we at home? elsif ( $current_ssid =~ /$home_ssid/ ) { # Is synergy running? if ( $synergy_status ) { # Is synergy running for home? if ( $synergy_location =~ /$home_server/ ) { #print "We're good!\n"; syslog('notice', "SSID:$current_ssid and location $synergy_location, doing nothing..."); } # The assumption is, if it's running but not in the right place, kill # the current process and restart it with the right info else { syslog('notice', "restarting synergy:$synergy_status and connecting to $home_server"); controlSynergy("restart", $home_server, $synergy_status); } exit; } else { syslog('notice', "starting synergy and connecting to $home_server"); controlSynergy("start", $home_server); } } # Otherwise don't connect to anything and stop the process else { syslog('notice', "stopping synergy:$synergy_status"); controlSynergy("stop"); } exit; ############ # Subs ############ sub controlSynergy { # We could have 1,2, or 3 arguments to this # the controlling operation, start, stop, or restart # the ip to use if we're starting or restarting # the current PID if we're stopping or restarting # if we have a "stop" but no PID #print Dumper @_; my ($command) = grep /(^start$|^stop$|^restart$)/i, @_; my ($pid) = grep /^(\d+)$/, @_; my ($host) = grep /^(\d+\.\d+\.\d+\.\d+)$/, @_; #print "$command\n" if $command; #print "$pid\n" if $pid; #print "$host\n" if $host; if ( $command =~ /^start$/i && defined $host ) { my $cmd = "$synergy_app -n $screen_name" . " " . $host . ":" . $synergy_server_port; #print "CMD: $cmd\n"; exec $cmd; } if ( $command =~ /^stop$/i && defined $pid ) { my $cmd = "kill -9 $pid"; #print "CMD: $cmd\n"; exec $cmd; } if ( $command =~ /^restart$/i && defined $pid && defined $host ) { my $cmd = "kill -9 $pid"; #print "CMD: $cmd\n"; system $cmd; $cmd = "$synergy_app -n $screen_name" . " " . $host . ":" . $synergy_server_port; #print "CMD: $cmd\n"; exec $cmd; } } sub getSynergyStatus { my $synergy_running = 0; my $synergy_location = 0; # Get a list of all processes my @full_proc_list = `ps -e`; my @synergy_process_list = grep /synergyc\s/, @full_proc_list; #print Dumper @synergy_process_list; #Did we find synergy in the process list? if ( @synergy_process_list ) { # Do a little error checking...sorta if ( scalar @synergy_process_list > 1) { print STDERR "Found multiple process runnning, taking the first\n"; print STDERR Dumper @synergy_process_list; } # Split the line into parts to find the PID my $process_line = shift @synergy_process_list; chomp $process_line; my @process_line_parts = split /\s+/, $process_line, 5; # Make sure we have all the parts if ( scalar @process_line_parts <5 ) { print STDERR "Couldn't identify all the parts of the process line\n"; print STDERR Dumper @process_line_parts; } # Get the PID for the synergy job my $synergy_pid; if ( $process_line_parts[0] =~ /\d+/ ) { $synergy_pid = $process_line_parts[0]; } else { $synergy_pid = $process_line_parts[1]; } print "PID = $synergy_pid\n"; # If we have a PID, we can assume its running and that value is > 0, so let's just shove it in here $synergy_running = $synergy_pid; # Get the options to the synergy executed command to determine how/where it's running my $synergy_command_line = $process_line_parts[4]; #print "$synergy_command_line\n"; if ( $synergy_command_line =~ /\s(\S+):24800$/ ) { #print "Synergy running at work\n"; $synergy_location = $1; chomp $synergy_location; } } # This will return 0 if not running and 0 for the location if it can be found return $synergy_running, $synergy_location; } sub getSSID { # Let's figure out what SSID we're currently connected to my $current_ssid = 0; # Run the airport tool my @airport_output = `$airport_app`; # Get the SSID line my @airport_ssid = grep /^\s+SSID:/, @airport_output; # This will be false if no SSID is found or Wifi is disabled if ( @airport_ssid ) { my $ssid_line = shift @airport_ssid; my ($blank, $label, $ssid) = split /\s+/, $ssid_line; #print "Current SSID: $ssid\n"; return $ssid; } else { #print STDERR "No associated SSID found\n"; return 0; } }