Link to home
Create AccountLog in
Avatar of robstarox
robstarox

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of robstarox
robstarox

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer