Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.00 KB | None | 0 0
  1. module YpChefserver
  2.   module LDAP
  3.     require 'net-ldap'
  4.     @ldap
  5.     def get_ldap(ldap_password)
  6.       if @ldap.nil?
  7.         @ldap = Net::LDAP.new :host => "ad.ypg.com",
  8.         :port => 389,
  9.         :auth => {
  10.               :method => :simple,
  11.               :username => "CN=svc_openaudit,OU=East Service Accounts,OU=System Accounts,DC=ad,DC=ypg,DC=com",
  12.               :password => "#{ldap_password}"
  13.         }
  14.       end
  15.       @ldap
  16.     end
  17.     def get_ldap_users(ldap_password)
  18.       filter = Net::LDAP::Filter.eq("cn", "DevOps")
  19.       treebase = "dc=ad, dc=ypg, dc=com"
  20.       get_ldap(ldap_password).search(:base => treebase, :filter => filter) do |entry|
  21.        #puts "DN: #{entry.dn}"
  22.        entry.each do |attribute, values|
  23.             return values if attribute == :member
  24.        end
  25.       end
  26.     end
  27.     def get_sam(ldap_password)
  28.       samacc = Array.new
  29.       get_ldap_users(ldap_password).entries.each{ |elem|
  30.         y = elem.to_s.split(/[,=]/)
  31.         filter = Net::LDAP::Filter.eq("cn", y[1])
  32.         treebase = "DC=ad,DC=ypg,DC=com"
  33.         get_ldap(ldap_password).search(:base => treebase, :filter => filter, :attributes => "SamAccountName") do |entry|
  34.           samacc << entry.samaccountname
  35.         end
  36.       }
  37.       return samacc
  38.     end
  39.     def get_attrs(ldap_password)
  40.       data = Hash.new
  41.       get_ldap_users(ldap_password).entries.each{ |elem|
  42.         y = elem.to_s.split(/[,=]/)
  43.         x = y[1]
  44.         filter = Net::LDAP::Filter.eq("cn", x)
  45.         treebase = "DC=ad,DC=ypg,DC=com"
  46.         attrs = ["mail", "givenname", "sn", "SamAccountName"]
  47.         get_ldap(ldap_password).search(:base => treebase, :filter => filter, :attributes => attrs) do |entry|
  48.           if ! entry[:mail][0].nil?
  49.             samid = entry.samaccountname[0].downcase
  50.             data[samid] = Hash.new
  51.             entry.each do |attribute, values|
  52.                 data[samid][attribute] = values[0]
  53.             end
  54.           end
  55.  
  56.         end
  57.       }
  58.       return data
  59.     end
  60.   end
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement