Eric Guo's blog.cloud-mes.com

Hoping writing JS, Ruby & Rails and Go article, but fallback to DevOps note

Query Active Directory Using Ruby Net Ldap Gems

Permalink

I’m trying devise_ldap_authenticatable during 2013 CNY, after spending a lot of time, found below script is quite usful if you want to using LDAP authenticate but found something wrong and start trying to debug.

Confirm first command line ldap works good
ldapsearch -x -LLL -D "SDCORP\mes.service" -w "Password" -b "DC=sdcorp,DC=global,DC=sandisk,DC=com" -s sub -H ldap://cvpcdcip04 "cn=Eric Guo" cn mail displayName samaccountname

Or for 2019 thape domain:

ldapsearch -x -LLL -D "THAPE\guochunzhong" -w "Password" -b "DC=thape,DC=com,DC=cn" -s sub -H ldap://server02.thape.com.cn "samaccountname=guochunzhong" cn mail displayName samaccountname
Using ruby gems to test
require 'net/ldap'
ldap = Net::LDAP.new :host => 'cvpcdcip04',
:port => 389,
:auth => {
:method => :simple,
:username => "MES Service",
:password => "Password"
}
filter = Net::LDAP::Filter.eq("cn", "MES Service")
# treebase must exactly match, otherwise can not found the entity
treebase = "DC=sdcorp,DC=global,DC=sandisk,DC=com"
res=ldap.search(:base => treebase, :filter => filter)
p ldap.get_operation_result

Comments