我们在实际编写Ruby语言代码时,在实现Ruby连接到LDAP的情况下,通常都可以使用到三种方法。在这里我们将会学到其中的两种。#t#
Ruby连接到LDAP代码示例:
- conn = LDAP::Conn.new("rsads02.foo.com")
- conn.bind("CN=username,CN=Users,DC=foo,
DC=com","password") do |bound| - bound.search("DC=foo,DC=com",
LDAP::LDAP_SCOPE_SUBTREE,"(&(name=*)
(objectCategory=person))",
['name','ipPhone']) do |user| - puts "#{user['name']} #{user['ipPhone']}"
- end
- end
- require 'net/ldap'
- ldap = Net::LDAP.new :host =>
server_ip_address, - :port => 389,
- :auth => {
- :method => :simple,
- :username =>"cn=manager,dc=example,dc=com",
- :password => "opensesame"
- }
- filter = Net::LDAP::Filter.eq(
"cn", "George*" ) - treebase = "dc=example,dc=com"
- ldap.search( :base => treebase,
:filter => filter )do |entry| - puts "DN: #{entry.dn}"
- entry.each do |attribute, values|
- puts " #{attribute}:"
- values.each do |value|
- puts " --->#{value}"
- end
- end
- end
- p ldap.get_operation_result
希望以上介绍的这些Ruby连接到LDAP的方法能够帮助大家。