need some help with Net::LDAP

Posted on 2008-11-05
Last Modified: 2013-12-24
Heres basically what Im trying to accomplish using Net::LDAP.   First thing I need to do is grab a specific user and get certain attributes for that user  such as email and name. (using ldap->search ) .   Second thing I need to do is store that data in a specific variable.   Heres the thing, when running an ldap query grabbing for certain attributes, it doesnt display it in a  nice format to parse it.  I need to parse it and grab data from the ldap search and store them as variables..  
         I read a little about Net::LDAP::LDIF, but im not quite sure how to store that data.  IF you look @ my code below, you can see what Im trying to accomplish. Basically the problem i will have is when the data returns,  its gonna return in a human readable format with something like this

dn: uid=me , ou=group,
cn:  Myfirstname Last

I want to parse and store the values of cn & mail only  so that
$email = ""
$name = "Myfirst Last"
The reason for this is Later in my script i have to call these values for posting it in a webpage...   Any help on this section would be greatly appreciated

Below is the code im working on.

1st section is just a test connection to see if i can get an answer with data in it. .  I copied this from online examples but I was unable to get back any data from the query..not quite sure why..  I am able to get data using ldapsearch command line but not via the script    here the results of the first set of code

sh-3.2# ./
0 entries returned

This a successful command line run using ldapsearch
sh-3.2# ldapsearch -z 0 -x -h -b "" uid=me mail cn

# LDAPv3
# base <> with scope subtree
# filter: uid=me
# requesting: mail cn

# Me,group,
dn: uid=me,ou=group,
cn: Last, Myfirst
cn: Myfirst Last

So why is my perl code not returning any info??

2nd section is how I am trying to store the values... I just need to figure out how to parse the data

#first script



#!/usr/bin/perl -w 


use Net::LDAP;

$ldap = Net::LDAP->new ( 'ldapserver' );

$ldap->bind or die $!;


$msg = ($ldap->search( base=> "" , attrs => [ "uid=me", "cn", "mail" ] ));

foreach $entry ($msg->entries) {$entry->dump;};

print "$msg\n";

if ( $msg->count() == 0) {

        print $msg->count(), " entries returned\n";

        foreach $entry ( $msg->all_entries()) {



print "$msg\n";



####################------------------End of Script



####################  start of 2nd script code


ldap = Net::LDAP->new ( 'ldapserver' );

$result = ($ldap->bind("uid=$data{username},ou=group,", password => $data{password}));


        if ($result->code() != LDAP_SUCCESS)    {

                $pagehead = $pagetitle ='INCORRECT USERNAME/PASSWORD';


                print "<p style=\"margin:10pt;\">INCORRECT USERNAME/PASSWORD\n.";

                exit 0; 



## Get some variables in place


$email = "$ldap->search (xxxx to grab just the email section  and awk '{print 2}' basically";  ##--not sure how to store this

$name  = "ldap->search (xxxx to grab just the name section  and awk '{print 2}' basically";  ##--or get the values in like this


$msg = $ldap->search ("uid=$data{username},ou=group,", attrs=> [ "cn", "mail" ] );


        if ($mesg->count() > 0) { foreach $entry ($msg->all_entries() ) {




Open in new window

Question by:robstarox
    1 Comment

    Accepted Solution

    i figured out what i was trying to accomplish

    ## Get some variables in place$ldap = Net::LDAP->new ( 'ldapserver' );
    $ldap->bind("uid=$data{username},", password => "$data{password}") or die ;
    $mesg = $ldap->search ( base => "" , filter => "uid=$data{username}" , attrs => [ 'sn', 'mail' ] );
        my $entry = $mesg->entry($index);
        my $email = $entry->get_value("mail");
        my $name = $entry->get_value("sn");

    Open in new window


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now