Advertisement
Guest User

auto-synergy-loc.pl

a guest
Nov 15th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.83 KB | None | 0 0
  1. !/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5. use Data::Dumper;
  6. use Sys::Syslog;
  7.  
  8. # Let's get some syslog action goin
  9. openlog('SYNERGY-LOCATION','', 'local0');
  10.  
  11.  
  12.  
  13. # Servers to connect to
  14. my $home_server = "192.168.0.50";
  15. my $work_server = "10.240.220.50";
  16.  
  17. # SSIDs we care about
  18. my $home_ssid = "HOME_SSID";
  19. my $work_ssid = "WORK_SSID";
  20.  
  21. # Synergy Options
  22. my $synergy_app = "/Applications/Synergy.app/Contents/MacOS/synergyc";
  23. my $synergy_server_port = "24800";
  24. my $screen_name = "LOCALHOSTNAME";
  25.  
  26. # Airport Utility
  27. my $airport_app = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I";
  28.  
  29.  
  30. ############
  31. # MAIN
  32. ############
  33.  
  34. my ($synergy_status, $synergy_location) = getSynergyStatus();
  35. #print "Synergy Status: $synergy_status @ $synergy_location\n";
  36.  
  37. my $current_ssid = getSSID();
  38. #print "Current SSID: $current_ssid\n";
  39.  
  40.  
  41. # Are we at work?
  42. if ( $current_ssid =~ /$work_ssid/ )
  43. {
  44.     # Is synergy running?
  45.     if ( $synergy_status )
  46.     {
  47.         # Is synergy running for work?
  48.         if ( $synergy_location =~ /$work_server/ )
  49.         {
  50.             #print "We're good!\n";
  51.             syslog('notice', "SSID:$current_ssid and location $synergy_location, doing nothing...");
  52.         }
  53.         # The assumption is, if it's running but not in the right place, kill
  54.         # the current process and restart it with the right info
  55.         else
  56.         {
  57.             syslog('notice', "restarting synergy:$synergy_status and connecting to $work_server");
  58.             controlSynergy("restart", $work_server, $synergy_status);
  59.         }
  60.         exit;
  61.     }
  62.     else
  63.     {
  64.         syslog('notice', "starting synergy and connecting to $work_server");
  65.         controlSynergy("start", $work_server);
  66.     }
  67. }
  68. # Are we at home?
  69. elsif ( $current_ssid =~ /$home_ssid/ )
  70. {
  71.     # Is synergy running?
  72.     if ( $synergy_status )
  73.     {
  74.         # Is synergy running for home?
  75.         if ( $synergy_location =~ /$home_server/ )
  76.         {
  77.             #print "We're good!\n";
  78.             syslog('notice', "SSID:$current_ssid and location $synergy_location, doing nothing...");
  79.         }
  80.         # The assumption is, if it's running but not in the right place, kill
  81.         # the current process and restart it with the right info
  82.         else
  83.         {
  84.             syslog('notice', "restarting synergy:$synergy_status and connecting to $home_server");
  85.             controlSynergy("restart", $home_server, $synergy_status);
  86.         }
  87.         exit;
  88.     }
  89.     else
  90.     {
  91.         syslog('notice', "starting synergy and connecting to $home_server");
  92.         controlSynergy("start", $home_server);
  93.     }
  94.    
  95. }
  96. # Otherwise don't connect to anything and stop the process
  97. else
  98. {
  99.     syslog('notice', "stopping synergy:$synergy_status");
  100.     controlSynergy("stop");
  101. }
  102.  
  103. exit;
  104.  
  105. ############
  106. # Subs
  107. ############
  108.  
  109. sub controlSynergy
  110. {
  111.     # We could have 1,2, or 3 arguments to this
  112.     # the controlling operation, start, stop, or restart
  113.     # the ip to use if we're starting or restarting
  114.     # the current PID if we're stopping or restarting
  115.     # if we have a "stop" but no PID
  116.     #print Dumper @_;
  117.    
  118.     my ($command) = grep /(^start$|^stop$|^restart$)/i, @_;
  119.     my ($pid)     = grep /^(\d+)$/, @_;
  120.     my ($host)    = grep /^(\d+\.\d+\.\d+\.\d+)$/, @_;
  121.    
  122.     #print "$command\n" if $command;
  123.     #print "$pid\n" if $pid;
  124.     #print "$host\n" if $host;
  125.    
  126.     if ( $command =~ /^start$/i && defined $host )
  127.     {
  128.         my $cmd = "$synergy_app -n $screen_name" . " " .  $host  . ":" . $synergy_server_port;
  129.         #print "CMD: $cmd\n";
  130.         exec $cmd;
  131.        
  132.     }
  133.    
  134.     if ( $command =~ /^stop$/i && defined $pid )
  135.     {
  136.         my $cmd = "kill -9 $pid";
  137.         #print "CMD: $cmd\n";
  138.         exec $cmd;
  139.     }
  140.    
  141.     if ( $command =~ /^restart$/i && defined $pid && defined $host )
  142.     {
  143.         my $cmd = "kill -9 $pid";
  144.         #print "CMD: $cmd\n";
  145.         system $cmd;
  146.         $cmd = "$synergy_app -n $screen_name" . " " .  $host  . ":" . $synergy_server_port;
  147.         #print "CMD: $cmd\n";
  148.         exec $cmd;
  149.     }
  150.    
  151. }
  152.  
  153.  
  154. sub getSynergyStatus
  155. {
  156.  
  157.     my $synergy_running  = 0;
  158.     my $synergy_location = 0;
  159.    
  160.     # Get a list of all processes
  161.     my @full_proc_list = `ps -e`;
  162.     my @synergy_process_list = grep /synergyc\s/, @full_proc_list;
  163.     #print Dumper @synergy_process_list;
  164.    
  165.     #Did we find synergy in the process list?
  166.     if ( @synergy_process_list )
  167.     {
  168.         # Do a little error checking...sorta
  169.         if ( scalar @synergy_process_list > 1)
  170.         {
  171.             print STDERR "Found multiple process runnning, taking the first\n";
  172.             print STDERR Dumper @synergy_process_list;
  173.         }
  174.        
  175.         # Split the line into parts to find the PID
  176.         my $process_line = shift @synergy_process_list;
  177.         chomp $process_line;
  178.         my @process_line_parts = split /\s+/, $process_line, 5;
  179.        
  180.         # Make sure we have all the parts
  181.         if ( scalar @process_line_parts <5 )
  182.         {
  183.             print STDERR "Couldn't identify all the parts of the process line\n";
  184.             print STDERR Dumper @process_line_parts;
  185.         }
  186.        
  187.         # Get the PID for the synergy job
  188.     my $synergy_pid;
  189.     if ( $process_line_parts[0] =~ /\d+/ ) {
  190.             $synergy_pid = $process_line_parts[0];
  191.     }
  192.     else {
  193.         $synergy_pid = $process_line_parts[1];
  194.     }
  195.         print "PID = $synergy_pid\n";
  196.        
  197.         # If we have a PID, we can assume its running and that value is > 0, so let's just shove it in here
  198.         $synergy_running = $synergy_pid;
  199.        
  200.         # Get the options to the synergy executed command to determine how/where it's running
  201.         my $synergy_command_line = $process_line_parts[4];
  202.         #print "$synergy_command_line\n";
  203.        
  204.         if ( $synergy_command_line =~ /\s(\S+):24800$/ )
  205.         {
  206.             #print "Synergy running at work\n";
  207.             $synergy_location = $1;
  208.             chomp $synergy_location;
  209.         }
  210.        
  211.     }
  212.    
  213.     # This will return 0 if not running and 0 for the location if it can be found
  214.     return $synergy_running, $synergy_location;
  215.  
  216. }
  217.  
  218.  
  219.  
  220. sub getSSID
  221. {
  222.  
  223.     # Let's figure out what SSID we're currently connected to
  224.     my $current_ssid = 0;
  225.    
  226.     # Run the airport tool
  227.     my @airport_output = `$airport_app`;
  228.    
  229.     # Get the SSID line
  230.     my @airport_ssid = grep /^\s+SSID:/, @airport_output;
  231.    
  232.     # This will be false if no SSID is found or Wifi is disabled
  233.     if ( @airport_ssid )
  234.     {
  235.        
  236.         my $ssid_line = shift @airport_ssid;
  237.         my ($blank, $label, $ssid) = split /\s+/, $ssid_line;
  238.         #print "Current SSID: $ssid\n";
  239.         return $ssid;
  240.        
  241.     }
  242.     else
  243.     {
  244.         #print STDERR "No associated SSID found\n";
  245.         return 0;
  246.     }
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement