Subversion Repositories sysadmin_scripts

Rev

Rev 148 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 148 Rev 175
Line 25... Line 25...
25
#       Added --version parameter to display version information
25
#       Added --version parameter to display version information
26
#       Created copyright using GNUv2
26
#       Created copyright using GNUv2
27
#    v0.2.1 - 20191209 RWR
27
#    v0.2.1 - 20191209 RWR
28
#       Since the openvpn cli does not return any exit codes (always 0), set it to monitor
28
#       Since the openvpn cli does not return any exit codes (always 0), set it to monitor
29
#       the logs, looking for. See sub verifyUp for details.
29
#       the logs, looking for. See sub verifyUp for details.
-
 
30
#    v0.2.2 - 20250430 RWR
-
 
31
#       If user enters a partial connection name (ie, doesn't match subdirectory of
-
 
32
#       $configDirs), use connection name as a filter to match existing. Basically
-
 
33
#       a poor man's search function
30
 
34
 
31
# On secure systems, you can set this up with the setuid bit to run as root
35
# On secure systems, you can set this up with the setuid bit to run as root
32
# chown root:root vpn
36
# chown root:root vpn
33
# chmod u+s vpn
37
# chmod u+s vpn
34
 
38
 
35
 
39
 
36
$main::VERSION = '0.2.1';
40
$main::VERSION = '0.2.2';
37
 
41
 
38
 
42
 
39
use Getopt::Long qw(:config auto_version bundling );
43
use Getopt::Long qw(:config auto_version bundling );
40
use Pod::Usage qw(pod2usage);
44
use Pod::Usage qw(pod2usage);
41
 
45
 
Line 105... Line 109...
105
   return $pid;
109
   return $pid;
106
}
110
}
107
   
111
   
108
# get all available sessions and their status
112
# get all available sessions and their status
109
# returns them in a hash
113
# returns them in a hash
-
 
114
# if $filter, use that to parse the names by matching it in the string
110
sub getSessions {
115
sub getSessions {
-
 
116
   my $filter = shift;
-
 
117
   $filter = '' unless $filter;
111
   my %sessions;
118
   my %sessions;
112
   my @possibleSessions = `ls $configDirs`;
119
   my @possibleSessions = `ls $configDirs`;
113
   my @active;
120
   my @active;
114
   chomp @possibleSessions;
121
   chomp @possibleSessions;
115
   @possibleSessions = grep{ -d "$configDirs/$_" } @possibleSessions;
122
   @possibleSessions = grep{ /$filter/ && -d "$configDirs/$_" } @possibleSessions;
116
   foreach my $thisSession ( @possibleSessions ) {
123
   foreach my $thisSession ( @possibleSessions ) {
117
      if ( $pid = &getPid( $thisSession ) ) {
124
      if ( $pid = &getPid( $thisSession ) ) {
118
         $sessions{$thisSession}{'pidFile'} = "$pidDir/$thisSession.pid";
125
         $sessions{$thisSession}{'pidFile'} = "$pidDir/$thisSession.pid";
119
         $sessions{$thisSession}{'logFile'} = "$logDir/$thisSession.log";
126
         $sessions{$thisSession}{'logFile'} = "$logDir/$thisSession.log";
120
         $sessions{$thisSession}{'statusFile'} = "$statusDir/$thisSession.status";
127
         $sessions{$thisSession}{'statusFile'} = "$statusDir/$thisSession.status";
Line 127... Line 134...
127
}
134
}
128
 
135
 
129
 
136
 
130
# displays all available sessions and their status
137
# displays all available sessions and their status
131
sub printSessions {
138
sub printSessions {
-
 
139
   my $filter = shift;
132
   my $sessions = &getSessions();
140
   my $sessions = &getSessions( $filter );
133
   print '-'x40 . "\nActive\tSession\t\tPID\n";
141
   print '-'x40 . "\nActive\tSession\t\tPID\n";
134
   foreach my $session ( sort keys %$sessions ) {
142
   foreach my $session ( sort keys %$sessions ) {
135
      print $$sessions{$session}{'pid'} ? "*" : " ";
143
      print $$sessions{$session}{'pid'} ? "*" : " ";
136
      print "\t$session" . ' 'x (length($session) < 25 ? 25 - length( $session ) : 25);
144
      print "\t$session" . ' 'x (length($session) < 25 ? 25 - length( $session ) : 25);
137
      if ( $$sessions{$session}{'pid'} ) {
145
      if ( $$sessions{$session}{'pid'} ) {
Line 186... Line 194...
186
sub startConnection {
194
sub startConnection {
187
   my $destination = shift;
195
   my $destination = shift;
188
   my $exitString = 'Unknown Exit Status';
196
   my $exitString = 'Unknown Exit Status';
189
   my $configFile = "$configDirs/$destination/$destination.ovpn";
197
   my $configFile = "$configDirs/$destination/$destination.ovpn";
190
   my $p12 =  "$configDirs/$destination/$destination.p12";
198
   my $p12 =  "$configDirs/$destination/$destination.p12";
-
 
199
   return '' unless -f "$configDirs/$destination"; # they did not give a known configuration
191
   chdir( "$configDirs/$destination" ) if $chdir;
200
   chdir( "$configDirs/$destination" ) if $chdir;
192
   if ( -f $configFile ) {
201
   if ( -f $configFile ) {
193
      # we found the config file
202
      # we found the config file
194
      if ( &getPid( $destination ) ) { # make sure it is not already running
203
      if ( &getPid( $destination ) ) { # make sure it is not already running
195
         return 'The connection was already active';
204
         return 'The connection was already active';
Line 273... Line 282...
273
   $status = ( $kill eq 'ALL' ) ? &killALL() : &killConnection( $kill );
282
   $status = ( $kill eq 'ALL' ) ? &killALL() : &killConnection( $kill );
274
   print "$status\n" unless $quiet;
283
   print "$status\n" unless $quiet;
275
} elsif ( $destination ) {
284
} elsif ( $destination ) {
276
   die "Start requires you to be root, use sudo\n" if $<;
285
   die "Start requires you to be root, use sudo\n" if $<;
277
   my $status =  &startConnection( $destination );
286
   my $status =  &startConnection( $destination );
278
   print "$status\n" unless $quiet;
287
   print "$status\n" if $status && !  $quiet;
-
 
288
   $show ||= !$status;
279
}
289
}
280
 
290
 
281
&printSessions() if $show;
291
&printSessions( $destination ) if $show;
282
 
292
 
283
1;
293
1;
284
 
294
 
285
__END__
295
__END__
286
 
296
 
Line 289... Line 299...
289
vpn
299
vpn
290
 
300
 
291
=head1 SYNOPSIS
301
=head1 SYNOPSIS
292
 
302
 
293
  vpn             Show status of all available sessions
303
  vpn             Show status of all available sessions
294
  vpn session     Start a session (must be root)
304
  vpn session     Start a session (must be root). If no config exists, show
-
 
305
                  possible matches
295
  vpn [options]
306
  vpn [options]
296
 
307
 
297
Controls a set of OpenVPN connections, starting, stopping, and auto-timeouts.
308
Controls a set of OpenVPN connections, starting, stopping, and auto-timeouts.
298
   
309
   
299
=head1 OPTIONS
310
=head1 OPTIONS