Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

ldapsearch in perl

by jose_m (Acolyte)
on Jan 07, 2013 at 22:24 UTC ( #1012121=perlquestion: print w/replies, xml ) Need Help??
jose_m has asked for the wisdom of the Perl Monks concerning the following question:

Hello Folks, i need to query ldapsearch and to see if ldap is running on a group of hosts. i an trying to use Net::LDAP then running $ldap. i am missing something. anyone feel like lending a hand

#!/usr/bin/perl use Net::LDAP; @hosts =qw[ host1 host2 host3]; sub Check { foreach (@hosts) { $ldap = Net::LDAP->new( "$_" ) or die "$@"; } }

ok for good measure here would be the linux command line for what i am trying to automate.

ldapsearch -x -H ldaps:// | wc -l

Replies are listed 'Best First'.
Re: ldapsearch in perl
by ravi6289 (Initiate) on Jan 08, 2013 at 04:36 UTC

    Hello Jose , You need to bind to the ldap server from there on , and use the count method to get the number of entries . Try including the below piece of code inside your subroutine,changing the $ldapserver to point to each of your hosts . The filter filter => "(objectClass=*)" , will get all of the entries Hope this helps !!!

    #!/usr/bin/perl use Net::LDAP; my $ldapserver = "<Server Name>"; my $ldapbase = "ou=people,ou=Urania,ou=Eng,dc=example,dc=com"; my $ldap = Net::LDAP->new( $ldapserver ) or die "$@"; my $mesg = $ldap->bind("cn=user>,ou=people,ou=Urania,ou=Eng,dc=example +,dc=com", password=>'<user>'); $mesg = $ldap->search( base => $ldapbase, filter => "(objectClass=*)") +; my @entries = $mesg->count; print "@entries\n"; $mesg = $ldap->unbind;

      thanks Ravi. that seemed to help, but the error has changed, i am not getting connection refused.

      Connection refused at line 15, <DATA> line 225

        ok folks so i decided to dump the module. all i needed was to make sure that i can bind so i settled for this: hope it helps someone:

        use IO::Socket; use Socket; my $port='3000'; $socket = new IO::Socket::INET->new(PeerAddr => $ip, PeerPort => $port, Proto => 'tcp', Timeout => 2, ) or print "could not create bind to $_. port $port: $@\n" and $e +xit_status=2 ; close($socket);

        you have to use gethostbyname to resolve the ip address for the given array of hosts. any questions, just ask.. thanks -jose.

Re: ldapsearch in perl
by linuxer (Curate) on Jan 07, 2013 at 23:36 UTC

    Is your code example complete?

    What is happening when you execute your code, what is not? Any warnings or errors?

    Your are defining a subroutine Check, but you never call it. So I assume you missed to call Check for execution.

    #! /usr/bin/perl use strict; use warnings; sub foo { print "I am foo().\n"; } # call foo or nothing (visible) will happen foo();

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012121]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-07-23 08:22 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (459 votes). Check out past polls.