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);

No comments:

Post a Comment