Subversion Repositories sysadmin_scripts

Rev

Rev 24 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#! /usr/bin/perl -w

my $configDirs = '/etc/openvpn';

sub getVPNSessions {
   my %sessions;

   @sessions = `ps ax | grep openvpn`;
   foreach my $session ( @sessions ) {
      my ($trash,$pid,$terminal,$flags,$time,@commands) = split( /\s+/, $session );
      my $command = join( ' ', @commands );
      next if $command =~ m/grep/;
      $command =~ m/^openvpn\s+([^.]+)\.ovpn/;
      $command = $1;
      $sessions{$command} = $pid;
   }
   return \%sessions;
}

my $user =  getlogin || getpwuid($<) || "";

my $destination = shift;

my $existingSessions = &getVPNSessions();
die "VPN Session already open for $destination (PID  $$existingSessions{$destination})\n" if ( $$existingSessions{ $destination } );

die "You must pass the name of the destination on the command line\n" unless $destination;
die "Could not locate configuration file in [$configDirs/$destination]\n" 
   unless -d "$configDirs/$destination";
chdir( "$configDirs/$destination" );
if ( -f "$destination.ovpn" ) {
   exec( "sudo openvpn $destination.ovpn");
} else {
   die "Could not open '$destination.ovpn' in directory '$configDirs/$destination'\n";
}
1;