rfransix has asked for the wisdom of the Perl Monks concerning the following question:

We can close the "building an ldif from 2 files"; let's focus on Perl coding.

Need help with this 'working' code: 1. returns employeeID DN correctly to STDOUT if I hardcode an employeeID value in filter. however, it has dashes above it, how to remove the dashes? 2. more importantly, I want the result to be sent to OUT, all I've been able to send there is Dumper structures 3. overall, within the file <ou3 is a list of employeeIDs, I want to read in each line, search, print out the DN and the next two lines (changetype and replace) to OUT; then read in each line from a second file <ou8, another list of employeeIDs, search, and print that DN to OUT (with \n at the end); until end of both ou3 and ou8 (they have an equal number of entries). Giving us an LDIF file to import to LDAP. Here's the code, thanks for looking and helping:

#! perl -slw use strict; use warnings; use constant batchdir => "c:\temp10"; use Net::LDAP; use Data::Dumper; open PERSON, "<ou3" or die $!; open MGR, "<ou8" or die $!; open OUT, ">buildAD.ldif" or die $!; my $HOST = "a"; my $ADMIN = "b"; my $PWD = "c"; my $BASEDN = "d"; my $ldap = Net::LDAP->new( "$HOST" ) or die "$@"; my $dn = $ldap->bind( "$ADMIN", password => "$PWD", version=>3 ); my @attr = "1.1"; while (<PERSON>) { $dn = $ldap->search( #return only the person DN base => "$BASEDN", filter => "(&(objectClass=user)(employeeID=$_))", scope => "sub", attrs => [@attr], ); $dn->code && warn $dn->error; foreach $dn ($dn->entries) { push @attr, $dn->dump; } my $ev = Data::Dumper->Dump([$dn], [qw(dn)]); print OUT $ev; #never writes anything to OUT print OUT changetype: modify; #never writes anything to OUT print OUT replace: manager; #never writes anything to OUT while (<MGR>) { $mgr = $ldap->search( #return only the manager DN base => "$BASEDN", filter => "(&(objectClass=user)(employeeID=$_))", scope => "sub", attrs => [@attr], ); $mgr->code && warn $mgr->error; foreach $mgr ($mgr->entries) { push @attr, $mgr->dump; } my $evev = Data::Dumper->Dump([$mgr], [qw(mgr)]); print OUT manager: $evev; } } $dn = $ldap->unbind; #session ends close OUT; close MGR; close PERSON;

20100616 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Perl Builds an LDIF
by NetWallah (Canon) on Jun 14, 2010 at 22:32 UTC
    Most likely, you have read in line-ending characters when you read the PERSON file.

    ldap probably cannot find the person, and returns an empty $dn.

    Fix by:

    while (<PERSON>) { chomp; # Strip trailing line-ending #.. process ldap stuff
    Your query would look much prettier, and be easier to answer if you followed Writeup Formatting Tips, and used code tags.

    Update: Fixed typo, and adding this recommendation:
    Use the 3-parameter doem of OPEN:

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

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

      Humble and grateful, your insightful observation to chomp has revived this perl program to a higher level. The resultant output remains divided by a dash line; and Data::Dumper and print OUT, these lines are not working as buildAD.ldif only gets ,,bless'' coding structure. foreach $dn ($dn->entries) { push @attr, $dn->dump; } #$Data::Dumper::Purity = 1; #$Data::Dumper::Deepcopy = 1; my $ev = Data::Dumper->Dump($dn, qw(dn)); print OUT $ev; }

      Humble and grateful, your insightful observation to chomp has revived this perl program to a higher level. The resultant output remains divided by a dash line still; and Data::Dumper and print OUT, these lines are not working as buildAD.ldif only gets ,,bless'' coding structure.

      foreach $dn ($dn->entries) { push @attr, $dn->dump; } #$Data::Dumper::Purity = 1; #$Data::Dumper::Deepcopy = 1; my $ev = Data::Dumper->Dump($dn, qw(dn)); print OUT $ev; }

        Grateful, I return with good news. I have removed the dashes, unwrapped the search result and sent to a file (although I had to hardcode the filename. My next plea is to discover why Net::LDAP::LDIF only writes the last result to FILE, I wish all results to write to FILE. Here's the updated code.

        while (<PERSON>){ chomp; $dn = $ldap->search( #return only the employeeID DN base => "$BASEDN", filter => "(&(objectClass=user)(employeeID=$_))", scope => "sub", attrs => ['1.1'] ); Net::LDAP::LDIF->new( "buildAD.ldif", "w", wrap=>40 )->write( $dn->ent +ries ); }