From: Jeffery H. <je...@uv...> - 2002-11-04 02:27:39
|
Hello! Chris, This is my code: #!/usr/bin/perl use Net::LDAP; use Net::LDAP::Util qw(ldap_error_text ldap_error_name ldap_error_desc ); $ldap = Net::LDAP->new("localhost") or die "Initial LDAP error"; $mesg = $ldap->bind( 'CN=HuangJeffery,CN=Users,DC=mydomain,DC=com', password => '1234' ); if ( $mesg->is_error ) { print "bind failed\n"; print ldap_error_name( $mesg->code ); print "\n"; } else { print "bind successful\n"; print ldap_error_name( $mesg->code ); print "\n"; } # if they don't pass an array of attributes... # set up something for them $searchString = "cn=HuangJeffery,cn=Users,dc=mydomain,dc=com"; if (!$attrs ) { $attrs = ['mobile' ]; } $result = $ldap->search ( scope => "sub", filter => "$searchString", attrs => $attrs ); if ( $result->code ) { print ldap_error_name($result->code); print "\n"; } else { my $href = $result->as_struct; # get an array of the DN names my @arrayOfDNs = keys %$href ; # use DN hashes # process each DN using it as a key foreach (@arrayOfDNs) { print $_,"\n"; my $valref = $$href{$_}; # get an array of the attribute names # passed for this one DN. my @arrayOfAttrs = sort keys %$valref; #use Attr hashes my $attrName; foreach $attrName (@arrayOfAttrs) { # skip any binary data: yuck! next if ( $attrName =~ /;binary$/ ); # get the attribute value (pointer) using the # attribute name as the hash my $attrVal = @$valref{$attrName} ; print "\t $attrName: @$attrVal \n"; } print "#-------------------------------\n"; # End of that DN } # # end of as_struct method # #-------- #------------ # # handle each of the results independently # ... i.e. using the walk through method my @entries = $result->entries; my $entr ; foreach $entr ( @entries ) { print "DN: ",$entr->dn,"\n"; #my @attrs = sort $entr->attributes; my $attr; foreach $attr ( sort $entr->attributes ){ #skip binary we can't handle next if ( $attr =~ /;binary$/ ); print " $attr : ",$entr->get_value($attr),"\n"; } #print "@attrs\n"; print "#-------------------------------\n"; } } $ldap->unbind; 1; Chris Ridd wrote: >On 3/11/02 3:23 am, Jeffery Huang <je...@uv...> wrote: > > > >>Dear all, >> I tried to write a simple program to query microsoft active >>directory data use Net::LDAP. >>I have binded it successful, but I cannot query it! I use the same >>command from ldap browser, >>it can query my data. my query string like that: >> >> $querystring = "cn=HuangJeffery,cn=Users,dc=mydomain,dc=com"; >> >>What's problem here! does anyone success to access AD use perl module? >>How to use it! >> >> > >I don't know what you mean by a query string. LDAP searches have search >bases and search filters (and a few other things) but no query strings. Can >you send us the exact parameters that you send to $ldap->search()? > >Cheers, > >Chris > > > |