my $ldif_OUT = Net::LDAP::LDIF->new( "buildAD.ldif", "a", wrap=>40 );
while (<PERSON>){
chomp;
$dn = $ldap->search( #return only the employeeID DN
base => "$BASEDN",
filter => "(&(objectClass=user)(employeeID=$_))",
scope => "sub",
attrs => ['1.1']
);
next unless $dn; # Make sure something is returned..
while( my $entry = $dn->pop_entry() )
{
$ldif_OUT->write_entry($entry);
}
print OUT "changetype: modify";
print OUT "replace: manager";
print OUT "manager: dn","\n";
}
$dn = $ldap->unbind; #session ends
(Minimalistic error-checking added..)
Syntactic sugar causes cancer of the semicolon. --Alan Perlis
| [reply] [d/l] |
Humbled, I return for wisdom. The while statements are erratic, my wish is to have a 1to1 relationship between the while statements and the input files, yet the 2nd while <MGR> will pull results for 50 lines before the first while <PERSON> runs again. I wish to read iteratively <PERSON> one line at a time, then print @lines, then read iteratively <MGR>, one line at a time, resulting in a four lines written to OUT or buildAD.ldif.
Also, perl throws an error on the second $_ use; how else may it be defined as I wish to place the next line from <MGR> file into the filter?
Error reads: Use of uninitialized value $_ in pattern match (m//) at build4.pl line 48, <MGR> line 120 (#1)(W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.
I am also puzzled why print OUT (@lines) does not print the contents of LDIFA after each line of <PERSON> is processed (instead it is printing erratically to OUT). And, why OUT fails in Net::LDAP::LDIF->new( "OUT", "a", wrap=>40 );, I am forced to hardcode "buildAD.ldif" instead of OUT.
my $ldap = Net::LDAP->new("$HOST", port=>389) or die "$@";
my $dn = $ldap->bind("$ADMIN", password=>"$PWD");
my $dnm = $ldap->bind("$ADMIN", password=>"$PWD");
my @attr = "1.1";
my $result = Net::LDAP::LDIF->new( "buildAD.ldif", "a", wrap=>40 );
my @lines = (<LDIFA>);
while (<PERSON>)
{
chomp;
$dn = $ldap->search( #return only the employeeID DN
base => "$BASEDN",
filter => "(&(objectClass=user)(employeeID=$_))",
scope => "sub",
attrs => [@attr]
);
next unless $dn; # Make sure something is returned
while( my $entry = $dn->pop_entry() )
{
$result->write_entry($entry);
if ( $result != /dn:/ )
{
print OUT (@lines);
}
while (<MGR>)
{
chomp;
$dnm = $ldap->search( #return only the manager DN
base => "$BASEDN",
filter => "(&(objectClass=user)(employeeID=$_))",
scope => "sub",
attrs => [@attr]
);
next unless $dnm; # Make sure something is returned
while( my $entry = $dnm->pop_entry() )
{
$result->write_entry($entry);
}
}
}
}
$dn = $ldap->unbind; #session ends
$dnm = $ldap->unbind; #session ends
close OUT2 or die "Cannot close in-memory file: $!";
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: $!";
close LDIFA or die "Cannot close in-memory file: $!";
| [reply] [d/l] |
while (<PERSON>)
{
chomp;
$dn = $ldap->search( #return only the employeeID DN
base => "$BASEDN",
filter => "(&(objectClass=user)(employeeID=$_))",
scope => "sub",
attrs => [@attr]
);
next unless $dn; # Make sure something is returned
while( my $entry = $dn->pop_entry() )
{
$result->write_entry($entry);
if ( $result != /dn:/ )
{
print OUT (@lines);
}
while (<MGR>)
{
...
could be reindented as
while (<PERSON>)
{
chomp;
$dn = $ldap->search( #return only the employeeID DN
base => "$BASEDN",
filter => "(&(objectClass=user)(employeeID=$_))",
scope => "sub",
attrs => [@attr]
);
next unless $dn; # Make sure something is returned
while( my $entry = $dn->pop_entry() )
{
$result->write_entry($entry);
if ( $result != /dn:/ )
{
print OUT (@lines);
}
while (<MGR>)
{
...
So, you'll see that once you've read to the end of MGR, you'll still have lines in PERSON. Maybe you want to look up data from the contents of MGR? Then a hash is a better structure:
my %manager;
while (<MGR>) {
$manager{ $_ } = $ldap->search(...); # return only the manager DN
};
while (<PERSON>) {
... # you never seem to use the information from MGR here...
};
| [reply] [d/l] [select] |
In addition to Corion's wise words,
Your code:
if ( $result != /dn:/ )
probably does not do what you think it does - and I'm curious to know what you think it does.
That is probably why you are puzzled about the contents out OUT.
You probably want to see if $entry contains "dn" and negate that:
if ( $entry !~ /dn:/ )
Syntactic sugar causes cancer of the semicolon. --Alan Perlis
| [reply] [d/l] [select] |
Grateful. I return with no good news. $result continues to clobber FILE, or possible print OUT clobbers FILE; either way, the desired output in an iterative format is not resolved. We pray for continued wisdom.
| [reply] |
I have reverted to the previous code, as I am getting no where with this exploration. This code gives me the DN from ou3 file to the OUT file, but once I hit the $entry = $dn->pop_entry(): line, it breaks again. If I do not use the pop_entry() method, each result sends the following to OUT (Net::LDAP::Search=HASH(0x1c72f1c)). Is there any other way around this problem? Thanks again.
#! 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, ">", "buildAD.ldif" or die "Cannot open 'buildAD.ldif': $!";
my $HOST = "11";
my $ADMIN = "cn=d";
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 @attr = "1.1";
my $result = Net::LDAP::LDIF->new( "buildAD.ldif", "a", wrap=>40 );
my $entry = <>;
while (<PERSON>){
chomp;
$dn = $ldap->search( #return only the employeeID DN
base => "$BASEDN",
filter => "(&(objectClass=user)(employeeID=$_))",
scope => "sub",
attrs => [@attr],
);
next unless $dn; #make sure something is returned
$entry = $dn->pop_entry();
$result->write_entry($entry);
print OUT "changetype: modify";
print OUT "replace: manager";
}
$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: $!";
| [reply] [d/l] |