Skip to content

Instantly share code, notes, and snippets.

@sinansh
Created June 30, 2018 05:14
Show Gist options
  • Save sinansh/2c15afa96de8109fc5f3312dbd870a23 to your computer and use it in GitHub Desktop.
Save sinansh/2c15afa96de8109fc5f3312dbd870a23 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'net/ldap'
def get_ldap_response(ldap)
msg = "Response Code: #{ ldap.get_operation_result.code }, Message: #{ ldap.get_operation_result.message }"
raise msg unless ldap.get_operation_result.code == 0
end
ldap = Net::LDAP.new :host => # your LDAP host name or IP goes here,
:port => # your LDAP host port goes here,
:encryption => :simple_tls,
:base => # the base of your AD tree goes here,
:auth => {
:method => :simple,
:username => # a user w/sufficient privileges to read from AD goes here,
:password => # the user's password goes here
}
search_param = # the AD account goes here
result_attrs = ["sAMAccountName", "displayName", "mail"] # Whatever you want to bring back in your result set goes here
# Build filter
search_filter = Net::LDAP::Filter.eq("sAMAccountName", search_param)
# Execute search
ldap.search(:filter => search_filter, :attributes => result_attrs) { |item|
puts "#{item.sAMAccountName.first}: #{item.displayName.first} (#{item.mail.first})"
}
get_ldap_response(ldap)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment