LDAP script to show available disks

by Juliet Kemp

We have an NFS system which involves part of the local disks of all desktops being exported via NFS. Mostly this is consistently accessed via /disk/machinename, but some desktops have more than one local directory that's exported. I finally got around recently to rewriting the (very old and no longer functional) script to query the LDAP database and get this info for a given machine name:

#!/usr/bin/perl -w 

use strict;
use Net::LDAPS;

die "Usage: showdisks machinename\n"
        unless (@ARGV == 1);

# Get & set values
my ($search) = @ARGV;
my $server   = "ldaps://ldap.example.com";
my $cert     = "/etc/ldap/servercert.pem";
my $base     = "dc=example,dc=com";

my $ldap = Net::LDAPS->new( $server,
                             verify => 'optional',
                             cafile => $cert ) or die $@;
my $mesg = $ldap->bind;

my $filter = "(nisMapEntry=*$search*")";
 
$mesg = $ldap->search(  base   => $base,
                        filter => $filter,
                        attr   => ['cn', 'nisMapEntry', 'nisMapName'],
                     );

$mesg->code && die $mesg->error;

my @entries = $mesg->sorted('nisMapEntry');

foreach my $entry ( @entries ) {
    my $location  = $entry->get_value( 'nisMapEntry' );
    my $automount = $entry->get_value( 'nisMapName' );
    my $dir       = $entry->get_value( 'cn' );

    # The if is because otherwise you get warnings from the first couple of lines 
    # of the LDAP return.  
    if ($dir) {
        my ($auto, $path)= split /_/,$automount;
        print "$location : /$path/$dir \n";
    }
}

$mesg = $ldap->unbind;
Hope it's useful to someone! If your LDAP automount info wasn't exported from NIS you may have different names for those attributes.