|
Net::OpenVPN::Manage - Manage an OpenVPN process via it's management port |
Net::OpenVPN::Manage - Manage an OpenVPN process via it's management port
Version 0.02
This module connects to the OpenVPN management interface, executes commands on the interface and returns the results or errors that result.
0.02:
1: Added the 'status_ref' method so you don't have to parse out the
values from the scalar data returned from the 'status' method
yourself.
2: Changed the 'status', 'log' and 'state' methods to return array
references instead of multi-line scalars when multi-line data
is returned.
3: Made several changes to the POD text to show examples and data
types returned.
use Net::OpenVPN::Manage;
my $vpn = Net::OpenVPN::Manage->new({
host=>'127.0.0.1',
port=>'1195',
password=>'password',
timeout=>10
});
# Error if unable to connect.
unless($vpn->connect()){
print $vpn->{error_msg}."\n";
exit 0;
}
# Get the current status table in version 2 format from the process. my $status = $vpn->status(2);
# If method returned false, print error message.
# Otherwise print table to STDOUT.
if ( ! $status ) {
print $vpn->{error_msg};
exit 0;
} else {
print $status."\n";
}
All the methods in this module will return 0, or boolean false if there is any error. In most cases an error message detailing the problem will be returned in $obj->{error_msg}.
For more extensive information on the use of the OpenVPN management commands referenced by these methods, see the OpenVPN documentation (http://www.openvpn.net) or at least the management help screen (print $vpn->help();).
# Sets auth-retry mode to 'none'
$vpn->auth_retry('none');
# Returns entire echo buffer
$vpn->echo('all');
# Prints the help screen to STDOUT
print $vpn->help();
# Releases the hold state on the OpenVPN process.
$vpn->hold('release');
# Prints current hold state.
print $vpn->hold();
# kills the connection with the common name of 'jsmith'
$vpn->kill('jsmith');
# kills the connection where the client is connecting from: '63.73.83.93:17023'
$vpn->kill('63.73.83.93:17023');
# prints the entire log buffer.
print @{$vpn->log('all')};
# turns off logging.
my $result = $vpn->log('off');
# Sets the log mute level to 10.
$vpn->mute(10);
# Sends SIGHUP signal to the process.
$vpn->signal('SIGHUP');
# Print all connection states
print @{$vpn->state()};
# Print the connection status page using the version 2 format.
print $vpn->status(2);
$r->{TITLE} is a scalar showing the status TITLE.
$r->{TIME} is a scalar showing the status TIME.
$r->{GLOBAL_STATS} is a scalar showing the GLOBAL_STATS line.
$r->{HEADER}{CLIENT_LIST} is an array of the CLIENT_LIST column headers.
$r->{HEADER}{ROUTING_TABLE} is an array of the ROUTING_TABLE column headers.
$r->{CLIENT_LIST} is an array of arrays containing connection data for each active connection.
[ ["John Doe", "1.2.3.4:5678", "4.3.2.1", "67264", "87264", "Fri Jul 7 14:20:51 2006", "1152300051"], > ["Jane Doe", "2.3.4.5:6789", "5.4.3.2", "12347101", "19043721", "Tue Jul 3 12:10:05 2006", "1150000050"] > ... ]
C<< $r->{ROUTING_TABLE} >> is an array of arrays containing ROUTING_TABLE data for each active connection.
This structure is identical to that in $r->{CLIENT_LIST}, with an array containing an array of values for each connection.
[ ["4.3.2.1", "John Doe" ,"1.2.3.4:5678", "Fri Jul 7 14:41:35 2006", "1152301295"], ["5.4.3.2", "Jane Doe" ,"2.3.4.5:6789", "Tue Jul 3 12:10:05 2006", "1150000050"] ... ]
my $r = $vpn->status_ref();
foreach my $array_ref ( @{$r->{CLIENT_LIST}} ){
print "Common Name: $$array_ref[0], bytes sent: $$array_ref[3], bytes recvd: $$array_ref[4]\n";
}
-- or to use the column heading ref --
my $r = $vpn->status_ref();
foreach my $array_ref ( @{$r->{CLIENT_LIST}} ){
print "$r->{HEADER}{CLIENT_LIST}[0]: $$array_ref[0], $r->{HEADER}{CLIENT_LIST}[3]: $$array_ref[3], $r->{HEADER}{CLIENT_LIST}[4]: $$array_ref[4]";
}
# Change verbosity level to 1.
$vpn->verb('1');
# Prints the version information to STDOUT.
print $vpn->version();
Example of CGI script that shows connected clients. Rough and definitely not pretty, just an example... Keep in mind that to extend this, you could have hyperlinks that callback and can disconnect sessions, print log entries, etc.
use strict;
use CGI;
use Net::OpenVPN::Manage;
my $cgi=CGI->new();
print $cgi->header();
my $vpn=Net::OpenVPN::Manage->new({host=>'openvpn.domain.com', port=>'1195', password=>'password', timeout=>'5'});
unless ($vpn->connect()){
print $vpn->{error_msg}."\n\n";
exit 0;
}
my $r=$vpn->status_ref();
print qq|<table border="1"><tr>|;
foreach my $heading ( @{$r->{HEADER}{CLIENT_LIST}} ){
print qq|<th>$heading</th>|;
}
print qq|</tr>|;
foreach my $aref ( @{$r->{CLIENT_LIST}} ){
print qq|<tr>|;
foreach my $r ( @{$aref} ){
print qq|<td>$r</td>|;
}
print qq|</tr>|;
}
print qq|</table>|;
Copyright (c) 2006 Aaron Meyer / MeyerTech.net
This module is free software; you can redistribute it or modify it under the same terms as Perl itself.
|
Net::OpenVPN::Manage - Manage an OpenVPN process via it's management port |