Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

working with DBI and Net::Ldap::Ldif

by philosophia (Sexton)
on Feb 16, 2006 at 21:51 UTC ( #530798=perlquestion: print w/replies, xml ) Need Help??

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

i'm working with DBI and Net::Ldap::Ldif - trying to write an app that populates a multidimensional array from a database query - then iterates through the data in the multidimensional array, writing to an ldif file. code is below, when i run it i get 'Entry 'username' is not a valid Net::LDAP::Entry object. at .line 96'
#!/opt/bin/perl # # NON-production program to generate ldif dump from mcdb table passwd # $Id$ # $Revision$ # $Source$ # use strict; $^W++; use DBI; use Getopt::Long; use Net::LDAP::LDIF; use Net::LDAP; use Net::LDAPS; #variables for options my $debug = ''; #debug mode my $passwd = ''; #update passwd info my $help = ''; #help msg my $verbose = ''; #verbose mode my $ldif = ''; #ldif file to write to my $entry = ''; #passwd entries my $i = ''; #counter my $fh = ''; #file handle #get the options and set up GetOptions( "help" => \$help, "debug" => \$debug, "verbose" => \$verbose, ); if ( $help ) { print <<EOH USAGE: [Options] --help - display this text --verbose - give stats on what we did on exit EOH ; exit(); } #connect to the db warn "Connecting to the database\n" if $debug; my $dbh = DBI->connect("dbi:Sybase:server=localhost;port=4100", "", "" +); $dbh->do("use mydb"); #get data from passwd table my $sth = $dbh->prepare( "select login,password,crypt,uid,gid,gecos,home,shell from nsit..p +asswd" ); #defines for field positions my $PWLOGIN = 0; my $PWPASSWORD = 1; my $PWCRYPT = 2; my $PWUID = 3; my $PWGID = 4; my $PWGECOS = 5; my $PWHOME = 6; my $PWSHELl = 7; $sth->execute() || die; #load up multidimensional array my $pwarray = $sth->fetchall_arrayref(); #load all results into mul +ti-dimensional array my %pwarrayindex; warn "Indexing Information ... \n" if $debug; for ( $i = 0; $i < scalar(@$pwarray); $i++ ) { if ( exists( $pwarrayindex{ $pwarray->[$i][0] } ) ) { $pwarrayindex{ $pwarray->[$i][0] } .= ",$i"; #add the index + number } else { $pwarrayindex{ $pwarray->[$i][0] } = $i; } } #open up ldif file for writing $ldif = Net::LDAP::LDIF->new( $fh, "w" ); #load up the ldif file for ( $i = 0; $i < scalar(@$pwarray); $i++ ) { print qq($pwarray->[$i][0]); print qq($pwarray->[$i][1]); print qq($pwarray->[$i][2]); print qq($pwarray->[$i][3]); print qq($pwarray->[$i][4]); print qq($pwarray->[$i][5]); print qq($pwarray->[$i][6]); print qq($pwarray->[$i][7]); $ldif->write($pwarray->[$i][0]);//string $ldif->write($pwarray->[$i][1]);/string $ldif->write($pwarray->[$i][2]);//string $ldif->write($pwarray->[$i][3]);//int $ldif->write($pwarray->[$i][4]);//int $ldif->write($pwarray->[$i][5]);//string $ldif->write($pwarray->[$i][6]);//string $ldif->write($pwarray->[$i][7]);//string } $ldif->done();
any advice appreciated.

Edited by planetscape - added readmore tags

Replies are listed 'Best First'.
Re: working with DBI and Net::Ldap::Ldif
by g0n (Priest) on Feb 17, 2006 at 08:40 UTC
    line 96 is

    which is the first line attempting to write to the LDIF file. You appear to be trying to write a string attribute value 'username'. Net::LDAP::LDIF expects the parameter to 'write' to be a valid Net::LDAP::Entry object. You need to construct a Net::LDAP::Entry object for every LDAP object you want to create in your LDIF file, and pass that to 'write'.


    use strict; use warnings; use Net::LDAP::LDIF; use Net::LDAP; my @entryvalues = qw( testuser testcn testsn ); my $entry = Net::LDAP::Entry->new(); $entry->dn("cn=".$entryvalues[1].",ou=test,dc=test,dc=com"); $entry->add( "uid" => $entryvalues[0], "cn" => $entryvalues[1], "sn" => $entry[2], "objectclass"=> "inetorgperson" ); my $ldif = Net::LDAP::LDIF->new("file.ldif","w"); $ldif->write($entry);


    "If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."
    John Brunner, "The Shockwave Rider".

    Can you spare 2 minutes to help with my research? If so, please click here

      thanks, that helped a lot.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://530798]
Approved by Corion
Front-paged by planetscape
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2022-05-24 18:01 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (84 votes). Check out past polls.