Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Configuration Constants (move to yaml ?)
- HOST = 'ad01.domain.local'
- PORT = 636
- USER = 'administrator'
- PASS = 'password'
- BASE = 'dc=domain, dc=local'
- DOMAIN = 'domain.local'
- SCOPE = '(&(objectCategory=person)(objectClass=user))'
- require 'rubygems'
- begin
- require 'net/ldap'
- rescue LoadError => err
- abort "Please run 'sudo gem install net-ldap' to enable connecting to LDAP."
- end
- begin
- require 'fastercsv'
- rescue LoadError => err
- abort "Please run 'sudo gem install fastercsv' to enable connecting to LDAP."
- end
- class CSVBuilder
- def initialize(head)
- if head.is_a?(Array)
- @head = head
- @rows = Array.new
- else
- raise 'Headers MUST be provided as an array'
- end
- end
- def add_row(row)
- if row.is_a?(Array)
- @rows << FasterCSV::Row.new(@head,row)
- else
- raise 'Row MUST be provided as an array'
- end
- end
- def export(filename='export.csv')
- f = File.open(filename,'w')
- f << FasterCSV::Table.new(@rows)
- f.close
- end
- end
- ldap = Net::LDAP.new(
- :host => HOST,
- :port => PORT,
- :base => BASE,
- :encryption => :simple_tls,
- :auth => { :username => "#{USER}@#{DOMAIN}",
- :password => PASS,
- :method => :simple }
- )
- users = ldap.search(:filter => SCOPE)
- csv = CSVBuilder.new(%w[username name email proxy])
- users.each do |user|
- user_name = user[:samaccountname]
- disp_name = user[:displayname]
- email_ady = user[:mail]
- proxy_ady = user[:proxyaddresses]
- csv.add_row([user_name, disp_name, email_ady, proxy_ady])
- end
- csv.export('ldap.csv')
Add Comment
Please, Sign In to add comment