Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- require 'net/ldap'
- HOST = "balmain.homeunix.org"
- PORT = 389
- BASE = "dc=balmain,dc=homeunix,dc=org"
- USER = "Manager"
- PASS = "secret"
- ldap = Net::LDAP.new
- ldap.host = HOST
- ldap.port = PORT
- ldap.base = BASE
- ldap.auth "cn=#{USER},#{BASE}", PASS
- ldap.open do |ldap|
- unless ldap.auth( "cn=#{USER},#{BASE}", PASS ) && ldap.bind
- raise( "Could not authenticate: #{ldap.get_operation_result.inspect}")
- end
- # add root node
- dn = BASE
- att = {
- :dc => "balmain",
- :objectclass => ["dcObject", "organizationalUnit"],
- :ou => "Balmain Dot Homeunix Dot org"
- }
- puts "Trying to add root node ..."
- ldap.add( :dn => dn, :attributes => att )
- puts ldap.get_operation_result.message
- # add people ou
- dn = "ou=people,#{BASE}"
- att = {
- :objectclass => "organizationalUnit",
- :ou => "people"
- }
- puts "Trying to add people ou ..."
- ldap.add( :dn => dn, :attributes => att )
- puts ldap.get_operation_result.message
- # add a person
- dn = "cn=David Lee,ou=people,#{BASE}"
- att = {
- :objectclass => ["top", "inetorgperson"],
- :cn => "David Lee",
- :sn => "Lee",
- :mail => "david@davelee.com.au",
- }
- puts "Trying to add person ..."
- ldap.add( :dn => dn, :attributes => att )
- puts ldap.get_operation_result.message
- # modify a person
- puts "Trying to modify person ... "
- att = {:mail => "david@rubyist.net.au"}
- ldap.modify( :dn => dn, :attributes => att )
- puts ldap.get_operation_result.message
- # add person attribute
- puts "Trying to add attribute to person ... "
- ldap.add_attribute( dn, :mobile, '0414 220 186' )
- puts ldap.get_operation_result.message
- # delete person attribute
- puts "Trying to delete attribute from person ... "
- ldap.delete_attribute( dn, :mobile )
- puts ldap.get_operation_result.message
- # delete a person
- puts "Trying to delete person ... "
- #ldap.delete( :dn => dn )
- #puts ldap.get_operation_result.message
- # show all LDAP entries
- puts "__________________________"
- attrs = ["mail", "cn", "sn", "objectclass"]
- ldap.search( :base => BASE, :attributes => attrs, :return_result => true ) do |entry|
- puts "LDAP Entry Distinguished Name: #{entry.dn}"
- if entry.respond_to?(:cn)
- puts "LDAP Entry Common Name: #{entry.cn rescue '-'}"
- end
- if entry.respond_to?(:mail)
- puts "Email addresses:"
- entry.mail.each {|ma| puts ma}
- end
- puts "\t#{entry.inspect}"
- end
- end
Add Comment
Please, Sign In to add comment