I love Free Software!

Pages

Funny Quotes to Think

When you say "I wrote a program that crashed Windows", people just stare at You blankly and say "Hey, I got those with the system, for free"

-Torvalds, Linus(1995-03-08)-

Wednesday, September 29, 2010

LDAP to CSV using perl



#!/usr/bin/perl
# This script will export LDAP entry in to a csv file. 
# Install Net:LDAP for running this script. 
# Feel free to contact me on praveen.velu84@yahoo.com

use Net::LDAP;

$ldap = Net::LDAP->new("localhost");
$ldap->bind("cn=admin,dc=example,dc=com", password=>"secret");

# Create a new file "ldapcsv.csv and pipe your query output to this file.  
$outputfile = "ldapcsv.csv";

# Create a new File Handler.
open (FH, ">$outputfile") or die "$!";

# Modify your search query and ldap connection details.
$mesg = $ldap->search(filter=>"(&(mail=*)(objectclass=*))", base=>"dc=example,dc=com");

@entries = $mesg->entries;
foreach $entry (@entries){
        @myuid = $entry->get( 'uid' );
        @givenName = $entry->get( 'givenName' );
        @sn = $entry->get( 'sn' );
        @userPassword = $entry->get( 'userPassword' );
        print "Exporting  @myuid[0] to csv file \n";
print FH "@myuid[0],@givenName[0],@sn[0],@userPassword[0]\n"
}

# Close the file handler.
close(FH);

Tuesday, September 7, 2010

Creating OpenVPN client certificates using perl script

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;