Getting closer. I've started over using new code from the examples. Without the
second while or LDAPsearchmgr code, we get the desired results using while
(<PERSON>) etc...:
dn: cn=me,dc=corp
changetype: modify
replace: manager
Unfortunately, attempts to insert a second while with LDAPsearchmgr returns
erratic results as the first while runs one line then the entire second while,
then concludes with the remainder of the first while.
How can this code be improved to iterate through PERSON one line at a time,
print the result, print the next two lines, then iterate through MGR one line
at a time, printing that result as the fourth line. Thanks for looking.
<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 MGR, "<", "ou8" or die "Cannot open 'ou8': $!";
open OUT, ">", "ldif3.ldif" or die "Cannot open 'ldif3.ldif': $!";
my $HOST = "1";
my $ADMIN = "cn=me,DC=corp";
my $PWD = "0";
my $BASEDN = "DC=corp";
my $ldap = Net::LDAP->new("$HOST", port=>389) or die "$@";
my $dn = $ldap->bind("$ADMIN", password=>"$PWD");
my $mgrdn = $ldap->bind("$ADMIN", password=>"$PWD");
sub LDAPsearch
{
my ($dn) = @_;
my $dns = $ldap->search( #return only the employeeID DN
base => "$BASEDN",
filter => "(&(objectClass=user)(employeeID=$_))",
scope => "sub",
attrs => ['1.1']
);
}
sub LDAPsearchmgr
{
my ($dnmgr) = @_;
my $dnsmgr = $ldap->search( #return only the employeeID DN
base => "$BASEDN",
filter => "(&(objectClass=user)(employeeID=$_))",
scope => "sub",
attrs => ['1.1']
);
}
while (<PERSON>) {
chomp;
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\n";
while (<MGR>) {
chomp;
my @Attrsmgr = ( );
my $resultsmgr = LDAPsearchmgr ($dn, \...@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 "Return Code: ", $mesg->code;
print "\tMessage: ", $mesg->error_name;
print " :", $mesg->error_text;
print "MessageID: ", $mesg->mesg_id;
print "\tDN: ", $mesg->dn;
}
}
}
$dn = $ldap->unbind; #session ends
close OUT or die "Cannot close in-memory file: $!";
close MGR or die "Cannot close in-memory file: $!";
close PERSON or die "Cannot close in-memory file: $!";