CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
By Split and By Hash and By Net::LDAP

 



rfransix
Novice

Jul 9, 2010, 10:11 AM

Post #1 of 1 (1471 views)
By Split and By Hash and By Net::LDAP Can't Post

Thanks for taking time to help here.

This code will print 4 lines building an ldif for an ldapmodify command (as long as employeeID is hardcoded in). The first line is the DN of each person's record, the 2nd and 3rd lines are pre-determined text, and the 4th line is the DN of the assigned person's manager. Both lines 1 and 4 do an ldap->search to return the DN value. If there are recommendations to reduce this logic, please let me know.

Here's the results:

dn: CN=Employee\, Richard,OU=people,DC=corp,DC=com
changetype: modify
replace: manager
manager: CN=Manager\, Ashley,OU=people,DC=corp,DC=com


Here's where I need help.

1. How to setup the split to use either one sub or both (use a while, a do, something else)
2. How to setup the error result logic.

We want to code this so that each search pre-populates the filter value of each ID by splitting a file that looks like this: 123456,789012, where 123456 is the LDAPsearch->employeeid and 789012 is the LDAPsearchmgr->employeeid (both filter on employeeid because that is the key we use). There are thousands of lines in this file. We want to code this up so if the search for 123456 returns an error or nothing, we do not print anything, skip to the next line, but print the number and an error statement to an error_file; if 789012 returns an error or nothing, we do not print anything, skip to the next line, but print the number and an error statement to an error_file.

Here's what I have so far. We want to replace both employeeids with employeeid=$_ Better yet, if we could use only one LDAPsearch subroutine by first splitting in the employeeid, then next splitting in the managerid, that would be ideal. But how?


Code
   #! perl       

use strict;
use warnings;
use diagnostics;

use Net::LDAP;
use Net::LDAP::Entry;
use Net::LDAP::Search;
use Net::LDAP::LDIF

open PERSON, "<", "ou3" or die "Cannot open 'ou3': $!";
open ERROR, ">", "errorfile" or die "Cannot open 'errorfile': $!";

my $HOST = "1.1.1.1";
my $ADMIN = "cn=admin,ou=people,DC=corp,DC=com ";
my $PWD = "secret";
my $BASEDN = "ou=people,DC=corp,DC=com";

my $ldap = Net::LDAP->new("$HOST", port=>389) or die "$@";
my $dn = $ldap->bind("$ADMIN", password=>"$PWD");
my $dnmgr = $ldap->bind("$ADMIN", password=>"$PWD");

sub LDAPsearch
{
my ($dn) = @_;
my $dns = $ldap->search( #return only the employeeID DN
base => "$BASEDN",
filter => "(&(objectClass=user)(employeeID=123456))",
scope => "sub",
attrs => ['1.1']
);
}

sub LDAPsearchmgr
{
my ($dnmgr) = @_;
my $dnsmgr = $ldap->search( #return only the managerID DN
base => "$BASEDN",
filter => "(&(objectClass=user)(employeeID=789012))",
scope => "sub",
attrs => ['1.1']
);
}

my @Attrs = ( );
my $results = LDAPsearch ($dn, \@Attrs );
my @entries = $results->entries;
my $entr;
foreach $entr ( @entries ) {
print "dn: ", $entr->dn, "\n";
my $attr;
foreach $attr ( sort $entr->attributes ) {
next if ( $attr =~ /;binary$/ );
print " $attr : ", $entr->get_value ( $attr ) ,"\n";
}
print "changetype: modify\n";
print "replace: manager\n";

my @Attrsmgr = ( );
my $resultsmgr = LDAPsearchmgr ($dnmgr, \@Attrsmgr );
my @entriesmgr = $resultsmgr->entries;
my $entrmgr;
foreach $entrmgr ( @entriesmgr ) {
print "manager: ", $entrmgr->dn, "\n";
my $attrmgr;
foreach $attrmgr ( sort $entrmgr->attributes ) {
next if ( $attrmgr =~ /;binary$/ );
print " $attrmgr : ", $entrmgr->get_value ( $attrmgr ) ,"\n";
}
}

if ( $results->code ) {
LDAPerror ( "Searching", $results );
}

sub LDAPerror
{
my ($from, $mesg) = @_;
print ERROR "Return Code: ", $mesg->code;
print ERROR "\tMessage: ", $mesg->error_name;
print ERROR " :", $mesg->error_text;
print ERROR "MessageID: ", $mesg->mesg_id;
print ERROR "\tDN: ", $mesg->dn;
}
}

$dn = $ldap->unbind; #session ends

close ERROR or die "Cannot close in-memory file: $!";
close PERSON or die "Cannot close in-memory file: $!";


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives