• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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

0
robstarox
Asked:
robstarox
1 Solution
 
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

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now