Netscape family directory index lists using ldapsearch & perl

Just a simple tutorial on how to get a list of indexes from a Netscape family directory server using ldapsearch.

ldapsearch -x -LLL -h ldap1 -D "cn=directory manager" -W 
-b "cn=index,cn=userRoot,cn=ldbm database,cn=plugins,cn=config" 
-s one "objectclass=nsindex"

This will just list all the indexes for the “userRoot” database. You can pipe it to a file (e.g. “>indexes.ldif”) or use the following code to create a csv formatted list (index types found in the second field are a pipe-delimited list):

#!/usr/bin/perl
use strict;
use Net::LDAP;
use Net::LDAP::Entry;

my $HOME = $ENV{'HOME'};
our($dirHost, $dirUsr, $dirPass);
require "$HOME/conf/scripts.conf";

my $outfile = "$HOME/data/userrootidx.csv";

my $basedn = "cn=index,cn=userRoot,cn=ldbm database,cn=plugins,cn=config";
my $query = "(objectclass=nsindex)";

my $ldap = Net::LDAP->new($dirHost);
my $mesg = $ldap->bind($dirUsr, password =>$dirPass) or die $!;

open FH, ">$outfile" or die $!;
print FH "Index Name,IndexTypes,SysIdxn";

$mesg = $ldap->search (
           base =>$basedn,
           scope => 'one',
           filter => $query
);

while (my $entry = $mesg->shift_entry()) {
      my $dn = $entry->dn;
      my $cn = $entry->get_value('cn');
      my @nsindextype = $entry->get_value('nsindextype');
      my $nssystemindex = $entry->get_value('nssystemindex');

      my $indextypestr;
      foreach my $indextype(@nsindextype) {
            $indextypestr = $indextype . "|" . $indextypestr;
      }
      for ($indextypestr) {
		s/^|//;
		s/|$//;
      }
      print FH "$cn,$indextypestr,$nssystemindexn";

}
$ldap->unbind;
close FH;
__END__;