24 |
rodolico |
1 |
#! /usr/bin/perl -w
|
|
|
2 |
|
|
|
3 |
my $configDirs = '/etc/openvpn';
|
|
|
4 |
|
|
|
5 |
sub getVPNSessions {
|
|
|
6 |
my %sessions;
|
|
|
7 |
|
|
|
8 |
@sessions = `ps ax | grep openvpn`;
|
|
|
9 |
foreach my $session ( @sessions ) {
|
|
|
10 |
my ($trash,$pid,$terminal,$flags,$time,@commands) = split( /\s+/, $session );
|
|
|
11 |
my $command = join( ' ', @commands );
|
|
|
12 |
next if $command =~ m/grep/;
|
|
|
13 |
$command =~ m/^openvpn\s+([^.]+)\.ovpn/;
|
|
|
14 |
$command = $1;
|
|
|
15 |
$sessions{$command} = $pid;
|
|
|
16 |
}
|
|
|
17 |
return \%sessions;
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
my $user = getlogin || getpwuid($<) || "";
|
|
|
21 |
|
|
|
22 |
my $destination = shift;
|
|
|
23 |
|
|
|
24 |
my $existingSessions = &getVPNSessions();
|
|
|
25 |
die "VPN Session already open for $destination (PID $$existingSessions{$destination})\n" if ( $$existingSessions{ $destination } );
|
|
|
26 |
|
|
|
27 |
die "You must pass the name of the destination on the command line\n" unless $destination;
|
|
|
28 |
die "Could not locate configuration file in [$configDirs/$destination]\n"
|
|
|
29 |
unless -d "$configDirs/$destination";
|
|
|
30 |
chdir( "$configDirs/$destination" );
|
|
|
31 |
if ( -f "$destination.ovpn" ) {
|
|
|
32 |
exec( "sudo openvpn $destination.ovpn");
|
|
|
33 |
} else {
|
|
|
34 |
die "Could not open '$destination.ovpn' in directory '$configDirs/$destination'\n";
|
|
|
35 |
}
|
|
|
36 |
1;
|