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

Posted on 2008-11-04
Medium Priority
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 = "john@domain.com"
## 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, o=domain.com" , 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
  • 2
  • 2
LVL 19

Accepted Solution

Kim Ryan earned 600 total points
ID: 22881800
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 http://search.cpan.org/~gbarr/perl-ldap-0.39/lib/Net/LDAP.pod#SYNOPSIS

Author Comment

ID: 22881903
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

by:Kim Ryan
ID: 22900015
The link I pointed you to (http://search.cpan.org/src/GBARR/perl-ldap-0.39/lib/Net/LDAP/Examples.pod
) 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

ID: 22900225
## Get some variables in place$ldap = Net::LDAP->new ( 'ldapserver' );
$ldap->bind("uid=$data{username}, o=domain.com", password => "$data{password}") or die ;
$mesg = $ldap->search ( base => "o=domain.com" , 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....


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Over time, the online landscape has altered considerably, but that’s nothing compared to the up-and-coming trends that will shape the web design industry in the coming year. Keep reading to find out which trends will shape B2B web design in 2018.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

807 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