Guest User

Untitled

a guest
Mar 3rd, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. require 'rubygems'
  2. require 'net/ldap'
  3.  
  4. HOST = "balmain.homeunix.org"
  5. PORT = 389
  6. BASE = "dc=balmain,dc=homeunix,dc=org"
  7.  
  8. USER = "Manager"
  9. PASS = "secret"
  10.  
  11. ldap = Net::LDAP.new
  12. ldap.host = HOST
  13. ldap.port = PORT
  14. ldap.base = BASE
  15. ldap.auth "cn=#{USER},#{BASE}", PASS
  16.  
  17. ldap.open do |ldap|
  18. unless ldap.auth( "cn=#{USER},#{BASE}", PASS ) && ldap.bind
  19. raise( "Could not authenticate: #{ldap.get_operation_result.inspect}")
  20. end
  21.  
  22. # add root node
  23. dn = BASE
  24. att = {
  25. :dc => "balmain",
  26. :objectclass => ["dcObject", "organizationalUnit"],
  27. :ou => "Balmain Dot Homeunix Dot org"
  28. }
  29. puts "Trying to add root node ..."
  30. ldap.add( :dn => dn, :attributes => att )
  31. puts ldap.get_operation_result.message
  32.  
  33. # add people ou
  34. dn = "ou=people,#{BASE}"
  35. att = {
  36. :objectclass => "organizationalUnit",
  37. :ou => "people"
  38. }
  39. puts "Trying to add people ou ..."
  40. ldap.add( :dn => dn, :attributes => att )
  41. puts ldap.get_operation_result.message
  42.  
  43. # add a person
  44. dn = "cn=David Lee,ou=people,#{BASE}"
  45. att = {
  46. :objectclass => ["top", "inetorgperson"],
  47. :cn => "David Lee",
  48. :sn => "Lee",
  49. :mail => "david@davelee.com.au",
  50. }
  51. puts "Trying to add person ..."
  52. ldap.add( :dn => dn, :attributes => att )
  53. puts ldap.get_operation_result.message
  54.  
  55. # modify a person
  56. puts "Trying to modify person ... "
  57. att = {:mail => "david@rubyist.net.au"}
  58. ldap.modify( :dn => dn, :attributes => att )
  59. puts ldap.get_operation_result.message
  60.  
  61. # add person attribute
  62. puts "Trying to add attribute to person ... "
  63. ldap.add_attribute( dn, :mobile, '0414 220 186' )
  64. puts ldap.get_operation_result.message
  65.  
  66. # delete person attribute
  67. puts "Trying to delete attribute from person ... "
  68. ldap.delete_attribute( dn, :mobile )
  69. puts ldap.get_operation_result.message
  70.  
  71. # delete a person
  72. puts "Trying to delete person ... "
  73. #ldap.delete( :dn => dn )
  74. #puts ldap.get_operation_result.message
  75.  
  76. # show all LDAP entries
  77. puts "__________________________"
  78.  
  79. attrs = ["mail", "cn", "sn", "objectclass"]
  80. ldap.search( :base => BASE, :attributes => attrs, :return_result => true ) do |entry|
  81. puts "LDAP Entry Distinguished Name: #{entry.dn}"
  82. if entry.respond_to?(:cn)
  83. puts "LDAP Entry Common Name: #{entry.cn rescue '-'}"
  84. end
  85. if entry.respond_to?(:mail)
  86. puts "Email addresses:"
  87. entry.mail.each {|ma| puts ma}
  88. end
  89. puts "\t#{entry.inspect}"
  90. end
  91. end
Add Comment
Please, Sign In to add comment