How this perl scrip will work
1. Accept certificate name through stdin
2. Create vpn certificate
3. Copy certificate and client.ovpn to a temp directory
4. Modify certificate name in client.ovpn configuration
5. Create a zip file of VPN certificate file
6. Remove temp file after creating archive
How can I run this script
1. Configure your openvpn server. You can check below link for configure openvpn
2. Copy the script to a file create_vpn_cert.pl
3. Modify script based on your open vpn configuration
4. Modify the permission to execute script
# chmod +x create_vpn_cert.pl
5. Execute the script
# perl create_vpn_cert.pl
#!/usr/bin/perl -w
# This perl script is used to create open vpn certificates.
# Script prompt for certificate name. based on the input script will create certificates /tmp directory
use warnings;
use File::Copy;
use Archive::Zip;
use File::Path;
my $zip = Archive::Zip->new();
# OUTPUT DIRECTORY FOR ZIP FILE
$outputdir='/tmp/';
# DIRECTORY WHERE client.ovpn is available
$ovpn = '/usr/share/doc/openvpn/examples/sample-config-files/';
#DIRECTORY WHERE KEYS ARE STORED
$keys = '/etc/openvpn/easy-rsa/2.0/keys/';
#DIRECTORY WHERE build-key SCRIPT AVAILABLE
$scriptdir = '/etc/openvpn/easy-rsa/2.0/';
#Creating Certificate
print "Enter VPN Certificate name:";
$certname = <STDIN>;
chomp $certname;
print "Creating Certificate $certname";
system ("$scriptdir/build-key $certname");
# Copy certificate files to temp folder
mkdir("/tmp/$certname", 0775) || print $!;
copy("$ovpn/client.ovpn","/tmp/$certname") or die "Copy failed: $!\n";
copy("$keys/ca.crt","/tmp/$certname") or die "Copy failed ca.crt: $!\n";
copy("$keys/$certname.crt","/tmp/$certname") or die "Copy failed $certname.crt: $!\n";
copy("$keys/$certname.key","/tmp/$certname") or die "Copy failed $certname.key: $!\n";
# Modifying client.ovpn file
my $filein = "/tmp/$certname/client.ovpn";
my $filetemp = $filein.'_'.$$;
open (my $fh_in, "<", $filein) or die;
open (my $fh_out, ">$filetemp") or die;
while (<$fh_in>) {
my $x = $_;
$x =~ s/cert client.crt/cert $certname.crt/g;
$x =~ s/key client.key/key $certname.key/g;
print $fh_out $x;
}
close ($filein);
close ($filetemp);
move("$filetemp","$filein");
# Creating archive
$zip->addTree( "/tmp/$certname" );
$zip->writeToFileNamed("/$outputdir/$certname.zip");
print "Created certficte for user in /$outputdir/$certname.zip \n";
# Removing tmp directory
rmtree("/tmp/$certname");
exit;