#!/bin/perl -w ############################################################################# # # $Source: /home/nickb/perl/ldap/RCS/search-1.03,v $ # $Revision: 1.1 $ # $Date: 2001/11/09 22:09:32 $ # $Author: nickb $ # $Locker: nickb $ # $State: Exp $ # # Purpose: search ldap database # # Directions: 'perldoc search' # # Default Location: # # Invoked by: # # Copyright (C) 2000 iEngineer.com # # This program is free software; you can redistribute it and/or # modify it under the terms of version 2 of the GNU General Public # License as published by the Free Software Foundation available at # # http://www.gnu.org/copyleft/gpl.html # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # ############################################################################# use 5.6.0; use Getopt::Std; use Net::LDAP; use strict; use vars qw($opt_b $opt_h $opt_p $opt_v $VERSION); $VERSION = 1.03; my $usage = q/ search -v search [ -b 'base DN' ] [ -h ldaphost ] [ -p ldapport ] filter [ parameter ]/ . "\n\n"; my $version = qq/search version $VERSION, Copyright (C) 2000 iEngineer.com This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\n\n/; die $usage if (@ARGV) == 0; # constants my $org_root = 'iengineer.com'; # program-wide vars my ($attr, $base, $entry, $host, @param, $port); getopts('b:h:p:v'); # ----------------------------------------------------------------------- # main # ----------------------------------------------------------------------- # check cmd-line params &chkOpts; die $usage unless $ARGV[0]; my $filter = shift; if ($ARGV[0]) { foreach $attr (@ARGV) { push(@param, $attr); } } my $paramref = \@param; my $ldap = Net::LDAP->new($host, port => $port); # anonymous bind to ldap server $ldap->bind; # search server my $mesg = $ldap->search ( base => $base, filter => $filter, attrs => [ @{$paramref} ] ) or die "search failed: $!\n" ; # print search results foreach $entry ($mesg->all_entries) { $entry->dump; } # end session $ldap->unbind; # ----------------------------------------------------------------------- # subroutine(s) # ----------------------------------------------------------------------- # ----------------------------------------------------------------------- # chkOpts: check cmd-line args # caller: main # parameters: # returns: # ----------------------------------------------------------------------- sub chkOpts { die $version if defined $opt_v; # -- base -- if (defined($opt_b)) { die $usage unless $opt_b =~ /\w+/; $base = $opt_b; } else { $base = 'o=iengineer.com'; } # -- host -- if (defined($opt_h)) { $host = $opt_h; } else { $host = 'localhost'; } # -- port -- if (defined($opt_p)) { die $usage unless $opt_p =~ /^\d+$/; $port = $opt_p; } else { $port = 389; } } # ----------------------------------------------------------------------- # documentation # ----------------------------------------------------------------------- =head1 NAME search - search ldap database =head1 SYNOPSIS B [ B<-b> C ] [ B<-h> ldaphost ] [ B<-p> ldapport ] filter [ C ] [ B<-v> ] =head1 DESCRIPTION I prints ldap objects to the screen based on criteria specified on the command line. =head1 OPTIONS =over 4 =item B<-b> base DN The DN that is the base object entry relative to which the search is to be performed. defaults to C unless specified. optionally change $org_root to an appropriate value. =item B<-h> ldaphost hostname or IP address of ldap server. =item B<-p> ldapport ldap server listen port. default 389. =item B attribute being searched for in ldap database. required. =item B<-v> display version and exit. =back =head1 EXAMPLES =over 4 =item example 1: print record pertaining to an attribute search -b o=iengineer.com -h thames uid=nickb =item example 2: print all records with common attribute search -b o=iengineer.com -h thames 'l=Salt Lake City' =item example 3: search for records using wildcard search -b o=iengineer.com -h thames uid=* =back =head1 PREREQUISITES perl v5.6.0 Net::LDAP Convert::ASN1 =head1 VERSION search v1.03, Copyright (C) 2000 iEngineer.com =head1 AUTHOR Nick Balthaser =head1 SCRIPT CATEGORIES UNIX/System_administration =head1 README search ldap database for specified attributes. =cut