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

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)

Also see
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.
Kim RyanIT ConsultantCommented:
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.
robstaroxAuthor Commented:
## 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....

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.