Docunext


Ruby LDAP

August 30th, 2009

This is example on how to use the ruby LDAP library:

#!/usr/bin/ruby
require 'ldap'
$MYHOST = '127.0.0.11'
base = 'dc=example,dc=com'
scope = LDAP::LDAP_SCOPE_SUBTREE
filter = '(objectclass=person)'
attrs = ['sn', 'cn', 'mail']
conn = LDAP::Conn.new($MYHOST, LDAP::LDAP_PORT)
conn.set_option( LDAP::LDAP_OPT_PROTOCOL_VERSION, 3 )
begin
  conn.search(base, scope, filter, attrs) { |entry|
     p entry.dn
     p entry.attrs
     p entry.vals('sn')
     p entry.vals('mail')
     p entry.to_hash
  }
rescue LDAP::ResultError
  conn.perror("search")
  exit
end
conn.perror("search")
conn.unbind
Yearly Indexes: 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2015 2019 2020 2022