I wrote some code for a similar purpose - this one provides a web-based form for query, and can be run as a CGI script.
#!c:/Perl/bin/perl.exe ## ## LDAP Users and groups, by Netwallah ## Aug, 2007 ## use CGI qw/:standard *table *Tr *td start_ul/; use Net::LDAP; use strict; # basic sequence with LDAP is connect, bind, search, interpret search # result, close connection my $ldapuser ="CN=LDAPREADONLY,OU= Service Accounts,OU=CityCORP,DC +=MyCompanyName,DC=com"; my $ldappassword ="LDAPPaassw00rd"; my $LDAP_SERVER = "DCCity5.MyCompanyName.com"; my $LDAP_ROOT_DN = "DC=MyCompanyName,DC=com"; my $NameWildCard = "A*"; # If unspecified, all "A's" will be listed. my @attributes = ("cn", "initials", "mail", "telephonenumber", "title", "employeetype", "employeenumber", "givenname", "displayname", "cn", "sn", "userAccountControl", "SAMAccountName" ); print header, start_html('MyCompanyName Active Directory' ), h3(" Active Directory query"), start_table({-border=>undef, -width=>'50%', -align=>'LEFT'}), Tr( td({-bgcolor=>'LIGHTGREEN'}, a({-href=>url(-base=>1) }, b(" +HOME")) ), td( "Please select or search for a USER or GROUP" ), ), end_table, "\n", br({-clear=>"left"}), # This piece is required, to fix renderi +ng problems ; print start_form; print "Search for:", textfield('"FORM_NAME_WILDCARD'), radio_group(-name=>'searchType', -values=>['user','group','computer'], -rows=>1,-columns=>3), submit('Lookup','go'),end_form,br(); print "(You can use * for wildcard searches, ex. *Stanley will find al +l Stanleys; st* will find all first & Last names starting with ST) < +BR>"; foreach my $letter ('A' .. 'Z' , 0..9) { print a({-href=>url(-relative=>1) . "?FORM_NAME_WILDCARD=$letter*" + }, b("$letter")); } if ( param('FORM_NAME_WILDCARD') ) { $NameWildCard= param 'FORM_NAME_WILDCARD' ; print h4("Searching for $NameWildCard"); }else{ print h4({align=>'center'},"Please select a start-letter for the se +arch, or type in the form above."); exit(); } my $filter = "(&(objectClass=user)(objectCategory=person)" . "(|(givenname=$NameWildCard)(sn=$NameWildCard)(SAMAcco +untName=$NameWildCard)))"; print "Connecting to $LDAP_SERVER ... " ; my $ldap=Net::LDAP->new($LDAP_SERVER) or die "$@"; # must be a valid +LDAP server! print "Binding ...\n"; my $ldap_bind_Result= $ldap->bind( $ldapuser, password => $ldappassword, version => 3 ); print "Bind result is [" . $ldap_bind_Result . "]<br />\n"; print "Searching for $filter ....\n"; # Search name entry ----- my $Search_Result= $ldap->search( base=> $LDAP_ROOT_DN, filter=>$filter, # scope => "sub", attrs =>\@attributes); $Search_Result->code && die $Search_Result->error; print $Search_Result->count() . " Entries .. Fetching them...\n"; #print "<CENTER><TABLE BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"10 +\" BGCOLOR=\"#FFFFEA\" >\n"; print start_table({-border=>undef, -width=>'80%', -align=>'CENTER',-ce +llspacing=>0, -cellpadding=>5, -bgcolor=>'LIGHTBLUE'}), "\n"; my $toggle = 0; foreach my $entr ( $Search_Result->entries() ) { if ($toggle = 1 - $toggle){ print start_Tr; } print start_td; print p("DN: ". $entr->dn. "\n"); foreach my $attr ( sort $entr->attributes ) { # skip binary we can't handle next if ( $attr =~ /;binary$/ ); print " $attr : ", $entr->get_value ( $attr ) ,"<br/>\n"; } print end_td; $toggle or print end_Tr; } print "\n",end_Tr, end_table,end_html; $ldap->unbind;

     Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)


In reply to Re: ldap search question by NetWallah
in thread ldap search question by csarid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.