philosophia has asked for the wisdom of the Perl Monks concerning the following question:
#!/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: ldif_parse_db_other.pl [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();
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 | |
by philosophia (Sexton) on Feb 17, 2006 at 15:44 UTC |