Ldap with ruby confusion

I have this piece of code that I modified based on search.rb in the ldap
package for Ruby.

Something isn’t working correctly with this code but I cannot find a way
to get the code to jsut printt regular ldap output instead of only
printing hashes or error messages when an exception is caught. Does
anyone know of a way that I can get just regular output that is similair
to just using the ldapsearch utility?

For example with ldapsearch -x -b ‘ou=People, o=University of
Washington, c=US’:

I get:
version: 2

···

filter: (objectclass=*)

requesting: ALL

search result

search: 2
result: 11 Administrative limit exceeded

numResponses: 1

A readable error…

Does anyone know how I would get this sort of readable error in Ruby
with the ldap package?

require ‘ldap’

HOST="directory.washington.edu"
BASE=“ou=People, o=University, c=US”

LDAP::Conn.new(HOST, LDAP::LDAP_PORT).bind{|conn|
conn.perror(“bind”)
begin
conn.search(BASE,
LDAP::LDAP_SCOPE_SUBTREE,
"(objectclass=*)"
){|e|
p e.keys
}
rescue LDAP::ResultError => msg
$stderr.print(msg)
end
}

Thanks,
Cere

Yes, but what server are you sending that query to? (It will be whatever is
in your ldap.conf). If I use the same server as your Ruby example, I get:

$ ldapsearch -h directory.washington.edu -x -b ‘ou=People, o=University of Washington, cs=US’
version: 2

···

On Wed, Jul 16, 2003 at 02:25:30PM +0900, Cere Davis wrote:

For example with ldapsearch -x -b ‘ou=People, o=University of
Washington, c=US’:

I get:
version: 2

filter: (objectclass=*)

requesting: ALL

search result

search: 2
result: 11 Administrative limit exceeded

filter: (objectclass=*)

requesting: ALL

search result

search: 2
result: 32 No such object <<<<<

numResponses: 1

Does anyone know how I would get this sort of readable error in Ruby
with the ldap package?

Running your example ruby-ldap program, I get:

bind: Success
No such object <<<<<

which is exactly the same as ldapsearch gives (except ldapsearch includes
the machine-parsable response as well - code 32

#define LDAP_NO_SUCH_OBJECT 0x20

So maybe it would be better if there were an LDAP exception object, or
different exceptions for each LDAP error, but apart from that it appears to
be doing the Right Thing [TM]. You are correctly capturing that result using
rescue.

Cheers,

Brian.

Try something like this:

#!/usr/bin/ruby -w

require ‘ldap’

HOST = ‘ldap.corp.foo.com
BASE = ‘ou=People,dc=foo,dc=com’

LDAP::Conn.new(HOST, LDAP::LDAP_PORT).bind do |conn|
begin
conn.search(BASE, LDAP::LDAP_SCOPE_SUBTREE, ‘(objectclass=*)’) do |entry|
entry.to_hash.each do |key, values|
values.each do |value|
printf(‘%s: %s\n’, key, value)
end
end
puts
end
end
end

That’ll do what you’re looking for, I think.

Ian

···

On Wed 16 Jul 2003 at 14:25:30 +0900, Cere Davis wrote:

I have this piece of code that I modified based on search.rb in the ldap
package for Ruby.

Something isn’t working correctly with this code but I cannot find a way
to get the code to jsut printt regular ldap output instead of only
printing hashes or error messages when an exception is caught. Does
anyone know of a way that I can get just regular output that is similair
to just using the ldapsearch utility?

require ‘ldap’

HOST=“directory.washington.edu”
BASE=“ou=People, o=University, c=US”

LDAP::Conn.new(HOST, LDAP::LDAP_PORT).bind{|conn|
conn.perror(“bind”)
begin
conn.search(BASE,
LDAP::LDAP_SCOPE_SUBTREE,
“(objectclass=*)”
){|e|
p e.keys
}
rescue LDAP::ResultError => msg
$stderr.print(msg)
end
}


Ian Macdonald | A committee is a life form with six or more
System Administrator | legs and no brain. – Lazarus Long, “Time
ian@caliban.org | Enough For Love”
http://www.caliban.org |
>