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

## 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

robstaroxAsked:
Who is Participating?
 
Kim RyanIT ConsultantCommented:
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)
{
    $entry->dump;
}

Also see http://search.cpan.org/~gbarr/perl-ldap-0.39/lib/Net/LDAP.pod#SYNOPSIS
http://search.cpan.org/src/GBARR/perl-ldap-0.39/lib/Net/LDAP/Examples.pod
0
 
robstaroxAuthor Commented:
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.
0
 
Kim RyanIT ConsultantCommented:
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.
0
 
robstaroxAuthor Commented:
## 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....

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.