Created
December 6, 2011 17:36
Usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/code $ ruby ldap_lister.rb | |
Please run 'sudo gem install net-ldap' to enable connecting to LDAP. | |
~/code $ sudo gem install net-ldap | |
Password: | |
Successfully installed net-ldap-0.2.2 | |
1 gem installed | |
~/code $ ruby ldap_lister.rb | |
Please run 'sudo gem install fastercsv' to enable connecting to LDAP. | |
~/code $ sudo gem install fastercsv | |
Successfully installed fastercsv-1.5.4 | |
1 gem installed | |
~/code $ ruby ldap_lister.rb | |
~/code $ open ldap.csv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment