in reply to searching LDAP and writing to text file

I do some LDAP programming occasionally, but I had never heard of Mozilla::LDAP. After looking on CPAN, I see that this module no longer appears to be supported since there hasn't been any documented changes since 1999. Unless you have some very good reason to stick with it, I would strongly recommend using Net::LDAP instead.

The code that you posted appears to be missing an else statement. You have a conditional, if (!$entry), to handle the case if no matches are found, but no corresponding else to handle the case if matches are found.

  • Comment on Re: searching LDAP and writing to text file

Replies are listed 'Best First'.
Re^2: searching LDAP and writing to text file
by mr_evans2u (Novice) on Oct 24, 2008 at 18:26 UTC
    I tried putting in an else, but still not getting anything.
    #!/usr/local/bin/perl -w # Modules used use Mozilla::LDAP::Conn; # Main "OO" layer for LDAP use Mozilla::LDAP::Utils; use Mozilla::LDAP::Entry; use Mozilla::LDAP::API qw(:constant :api); use Date::Calc qw(:all); use Time::Local; use strict; use English; use locale; use FileHandle; ############################################################## ### Configuration Block #1 Dev, Test & Production Systems ### ############################################################## my $script_log = "Migrate_functionals"; my $functional_data_file = "functional.txt"; my $pager_data_file = "pager.txt"; my $log_directory = "/usr/users/xxxx/"; my $data_directory = "/usr/users/xxxx/"; # UM connection variables my (%ld) = Mozilla::LDAP::Utils::ldapArgs(); my (%adld) = Mozilla::LDAP::Utils::ldapArgs(); $ld{port} = 389; $ld{root} = "ou=people,o=xxx.com,o=wcom"; $ld{base} = "ou=people,o=xxx.com,o=wcom"; $ld{scope} = "subtree"; my $UM_conn; my @attr = ("corpid", "aliasdate", "mail"); my $um_user_type; system ("mv $log_directory$script_log $log_directory$script_log.old"); open(LOG, ">$log_directory$script_log") || die "cannot create log f +ile $script_log\n"; if ($ARGV[0] eq "TEST") { $ld{host} = "xxxxxxxx.com"; $ld{bind} = "cn=xxxx"; $ld{pswd} = "xxx"; } elsif ($ARGV[0] eq "PROD") { $ld{host} = "xxxxxx.com"; $ld{bind} = "cn=xxxxxxxx"; $ld{pswd} = "xxxx"; } else { usage(); } } print "Connecting to UM...\n"; #################################################################e # Try to connect to the LDAP directory ################################################################## $UM_conn = new Mozilla::LDAP::Conn(\%ld); if (!$UM_conn) { print "UM Access failed. Exiting."; exit; } ################################################################### # Look up user(s) in Directory ###################################################################### +##### my $entry = $UM_conn->search($ld{base}, $ld{scope}, "(&(|(usertype=F)( +usertype=P))(mail=*)(!(espid=4)))", 0, @attr); #my $entry = $UM_conn->search($ld{base}, $ld{scope}, "(&(mail=*)(maila +lternateaddress=*)(aliasdate=*))", 0, @attr); if (!$entry) { my $code = $UM_conn->getErrorCode(); if ($code == 0) { print "No Directory records found to process on this run.\n" } else { print "Run terminated on LDAP Error=" . $UM_conn->getErrorStri +ng() . ".\n"; }else { while($entry) { my $functional = new FileHandle; my $pager = new FileHandle; open(FLOG, ">>$log_directory$functional_data_file") || die "can +not create Funtional file $functional_data_file\n"; open(PLOG, ">>$log_directory$pager_data_file") || die "cannot c +reate Funtional file $pager_data_file\n"; if (($um_user_type =~ /F/)) { print FLOG "$entry->{usertype}[0] - Address $entry->{mail} +[0]\n"; } elsif ($um_user_type =~ /P/) { print PLOG "$entry->{usertype}[0] - Address $entry->{mail}[0]\ +n"; } } $UM_conn->close; print STDERR "closing connection to UM"\n; }
      When I downloaded your code and opened it in an editor, I found two bracketing errors. Carefully review your code and make sure that each curly bracket is paired up properly and in the correct place.
        reran the code, was able to only get 1 account back even though I know there are hundreds more. When using this ldapsearch: ldapsearch -h server -D "cn=xxxx" -w "xxxxx" -b "ou=functionals,o=xxxx.com,o=com" -s sub '(&(|(usertype=F))(mail=*))' It's still not writing to the log either.
        #!/usr/local/bin/perl -w # Modules used use Mozilla::LDAP::Conn; # Main "OO" layer for LDAP use Mozilla::LDAP::Utils; use Mozilla::LDAP::Entry; use Mozilla::LDAP::API qw(:constant :api); use Date::Calc qw(:all); use Time::Local; use strict; use English; use locale; use FileHandle; # UM connection variables my (%ld) = Mozilla::LDAP::Utils::ldapArgs(); $ld{port} = 389; $ld{root} = "o=xxx.com,o=xxx"; $ld{base} = "ou=functionals,o=xxx.com,o=xxx"; $ld{scope} = "subtree"; my $UM_conn; my @attr = ("corpid", "usertype", "mail"); my $script_log = "Migrate_functionals"; my $functional_data_file = "functional.txt"; my $pager_data_file = "pager.txt"; my $log_directory = "/usr/users/xxx/"; my $data_directory = "/usr/users/xxxx/"; if ($ARGV[0]) { # determine environment # TODO: bind and password will be different for TEST vs. NA if ($ARGV[0] eq "TEST") { $ld{host} = "xxxxxx.com"; $ld{bind} = "cn=xxxxxxx"; $ld{pswd} = "xxxx"; } elsif ($ARGV[0] eq "PROD") { $ld{host} = "xxxxxx.com"; $ld{bind} = "cn=xxxxx"; $ld{pswd} = "xxxxxx"; } else { usage(); } } else { usage(); } print "Connecting to UM...\n"; #################################################################e # Try to connect to the UM LDAP directory ################################################################## $UM_conn = new Mozilla::LDAP::Conn(\%ld); if (!$UM_conn) { print "UM Access failed. Exiting."; exit; } #################################################################e my $record_count = 0; # How many entries we examine my $processed_record_count = 0; # How many entries we take action o +n ################################################################### # Look up user(s) in UM Directory ################################################################### # TODO: that filter is functionals not on Exchange my $entry = $UM_conn->search($ld{base}, $ld{scope}, "(&(|(usertype=F)) +(mail=*)(!(espid=4)))", 0, @attr); my $um_user_type; if (!$entry) { my $code = $UM_conn->getErrorCode(); if ($code == 0) { print "No Directory records found to process on this run.\n" } else { print "Run terminated on LDAP Error=" . $UM_conn->getErrorStri +ng() . ".\n"; } $UM_conn->close; } else { while($entry) { $record_count++; # TODO: create contact record in AD instead of migrating umaliasda +te my $functional = new FileHandle; my $pager = new FileHandle; open(FLOG, ">>$log_directory$functional_data_file") || die "can +not create Funtional file $functional_data_file\n"; if (($um_user_type =~ /F/)) { print FLOG "$entry->{usertype}[0] - Address $entry->{mail} +[0]\n"; print "Functional- $entry->{usertype}[0] - Address $entry +->{mail}[0]\n"; } else{ $entry = $UM_conn->nextEntry(); } last; } # End of while loop $UM_conn->close; print "\nRecords Examined: $record_count\n"; print "Records Updated: $processed_record_count\n\n"; print "Completed normally. $record_count records examined. $proces +sed_record_count records updated.\n"; } # End of else ($entry) 1; sub usage () { print "Usage: $0 [DEV|TEST|PROD] 2> logfile.log\n"; print " DEV - development environment\n"; print " TEST - test environment\n"; print " PROD - production environment\n"; print " logfile.log - the logfile can be used to reverse the + update\n"; print " when fed to the unmigrate_umaliasdate. +pl program.\n"; exit; }
        I ran this code and got: Use of uninitialized value at ./migrate_other_functionals_test2.pl line 135. Records Examined: 1 Records Updated: 0 Completed normally. 1 records examined. 0 records updated. I don't understand why it is saying $um_user_type is not initialized. And why it only found 1 account when I know there are hundreds. When I do an ldapsearch: ldapsearch -h csdir02 -D "cn=xxxxxxx" -w "xxx" -b "ou=functionals,o=xxxx.com,o=xxxx" -s sub 'umusertype=F' I get a ton back.
        #!/usr/local/bin/perl -w # Modules used use Mozilla::LDAP::Conn; # Main "OO" layer for LDAP use Mozilla::LDAP::Utils; use Mozilla::LDAP::Entry; use Mozilla::LDAP::API qw(:constant :api); use Date::Calc qw(:all); use Time::Local; use strict; use English; use locale; use FileHandle; # UM connection variables my (%ld) = Mozilla::LDAP::Utils::ldapArgs(); $ld{port} = 389; $ld{root} = "ou=people,o=xxx.com,o=xxx"; $ld{base} = "ou=functionals,o=xxxx.com,o=xxxx"; $ld{scope} = "subtree"; my $UM_conn; my @attr = ("corpid", "umaliasdate", "mail"); my $script_log = "Migrate_functionals"; my $functional_data_file = "functional.txt"; my $pager_data_file = "pager.txt"; my $log_directory = "/usr/users/xxxxxx/"; my $data_directory = "/usr/users/xxxxxxx/"; # OW connection variables my (%owld) = Mozilla::LDAP::Utils::ldapArgs(); $owld{root} = "o=corp"; $owld{base} = "o=corp"; $owld{bind} = "cn=UM account, ou=special accounts, o=corp"; $owld{pswd} = "xxxxxxxxx"; $owld{scope} = "subtree"; my $OW_conn; if ($ARGV[0]) { # determine environment if ($ARGV[0] eq "TEST") { $ld{host} = "xxxxxxxxxxx.com"; $ld{bind} = "cn=xxxxx"; $ld{pswd} = "xxxxxxx"; $owld{host} = "xxxxxxxx.com"; $owld{port} = 3389; } elsif ($ARGV[0] eq "PROD") { $ld{host} = "xxxxxx.com"; $ld{bind} = "cn=xxxxxx"; $ld{pswd} = "xxxxxxx"; $owld{host} = "xxxxx.com"; $owld{port} = 2389; } else { usage(); } } else { usage(); } print "Connecting to UM...\n"; #################################################################e # Try to connect to the UM LDAP directory ################################################################## $UM_conn = new Mozilla::LDAP::Conn(\%ld); if (!$UM_conn) { print "UM Access failed. Exiting."; exit; } #################################################################e my $record_count = 0; # How many entries we examine my $processed_record_count = 0; # How many entries we take action o +n ################################################################### # Look up user(s) in UM Directory ################################################################### # TODO: change the filter to '(&(|(usertype=F)(usertype=P))(mail=*)(!( +umespid=4)))') # TODO: that filter is functionals or pagers not on Exchange my $entry = $UM_conn->search($ld{base}, $ld{scope}, "(&(|(usertype=F)) +(mail=*)(!(umespid=4)))", 0, @attr); my $um_user_type; if (!$entry) { my $code = $UM_conn->getErrorCode(); if ($code == 0) { print "No Directory records found to process on this run.\n" } else { print "Run terminated on LDAP Error=" . $UM_conn->getErrorStri +ng() . ".\n"; } $UM_conn->close; } else { while($entry) { $record_count++; my $functional = new FileHandle; my $pager = new FileHandle; open(FLOG, ">>$log_directory$functional_data_file") || die "can +not create Funtional file $functional_data_file\n"; if (($um_user_type =~ /F/)) { print FLOG "$entry->{usertype}[0] - Address $entry->{mail} +[0]\n"; print "Functional- $entry->{usertype}[0] - Address $entry +->{mail}[0]\n"; } else{ $entry = $UM_conn->nextEntry(); } last; } # End of while loop $UM_conn->close; print "\nRecords Examined: $record_count\n"; print "Records Updated: $processed_record_count\n\n"; print "Completed normally. $record_count records examined. $proces +sed_record_count records updated.\n"; } # End of else ($entry) 1; sub usage () { print "Usage: $0 [DEV|TEST|PROD] 2> logfile.log\n"; print " DEV - development environment\n"; print " TEST - test environment\n"; print " PROD - production environment\n"; print " logfile.log - the logfile can be used to reverse the + update\n"; print " when fed to the unmigrate_umaliasdate. +pl program.\n"; exit; }