Perl LDAP NET::LDAP search queries, trying to store them as a variable

Posted on 2008-11-04
Last Modified: 2013-12-25
## Im trying to pull values from the ldap server.  The user is already authenticated as itself, but I want to pull out values for their account and store them as variables.  Obviously the syntax below is incorrect.  At this point in the code,  the ldap->bind is already bound, and i want to run a query with that bound information

I know how to do an ldapsearch command, but not familiar enough with the Net::LDAP to know how to parse it within perl..   For the values, I want to strip out  or Filter just the relevant info so that I get  these results.  I sort of want to awk/grep for specific return values.

$user = "John Doe"
$email = ""
## Get some variables in place from the ldap server so we can continue on with relevant user info


$email = "$data{username}\@domain\.com";   ##<-----   want to change this also to an ldap query

$name  = ($ldap->search ("uid=$data{username},ou=group," , cn));   #<---this section, how to i just pull out the username only



print "<p style=\"margin:10pt;\">$name </p>\n";    <----------this just prints out the data value for $name, but on the webpage is just shows up as    	Net::LDAP::Search=HASH(xxxxxxx).   

Open in new window

Question by:robstarox
    LVL 19

    Accepted Solution

    That's right, search return a hash of all the name/value pairs for the match. YOu need to unpack the hash like so,

    foreach $entry ($name->entries)

    Also see

    Author Comment

    So how can i print out the values returned from the ldap search on the webpage in that section of code?....   need help withing my example code.
    LVL 19

    Expert Comment

    The link I pointed you to (
    ) includes several detailed examples by the author of this module on how to use it. The examples are very similar to the type of problem you are trying to solve, and should be fairly easy to adapt with a bit of trial and error.

    I prefer to point people in the right direction rather than write every line of code for you. It means you get a better understanding of the problem. Can't see why this should be deleted.

    Author Comment

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

    Thanks!!   it took a while but i figured it out....


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now