need some help with Net::LDAP

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, o=domain.com
cn:  Myfirstname Last
mail:  me@domain.com

I want to parse and store the values of cn & mail only  so that
$email = "me@domain.com"
$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# ./ldap.pl
Net::LDAP::Search=HASH(0x8e1a34)
0 entries returned
Net::LDAP::Search=HASH(0x8e1a34)

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

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

# Me,group, domain.com
dn: uid=me,ou=group, o=domain.com
mail: me@domain.com
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=> "o=domain.com" , 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()) {
                $entry->dump();
        }
print "$msg\n";
}
 
####################------------------End of Script
 
 
####################  start of 2nd script code
 
ldap = Net::LDAP->new ( 'ldapserver' );
$result = ($ldap->bind("uid=$data{username},ou=group, o=domain.com", password => $data{password}));
 
        if ($result->code() != LDAP_SUCCESS)    {
                $pagehead = $pagetitle ='INCORRECT USERNAME/PASSWORD';
                &PrintHead;
                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,o=domain.com", attrs=> [ "cn", "mail" ] );
 
        if ($mesg->count() > 0) { foreach $entry ($msg->all_entries() ) {
                                                $entry->dump();
                                                }
                                }

Open in new window

robstaroxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

robstaroxAuthor Commented:
i figured out what i was trying to accomplish



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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

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.