2011-05-20 09:20:49 -05:00
|
|
|
'''This script allows to check a LDAP connection.'''
|
|
|
|
import sys, ldap
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class LdapTester:
|
2011-06-02 05:20:15 -05:00
|
|
|
'''Usage: python checkldap.py ldapUri login password base attrs filter
|
|
|
|
|
|
|
|
ldapUri is, for example, "ldap://127.0.0.1:389"
|
|
|
|
login is the login user DN, ie: "cn=gdy,o=geezteem"
|
|
|
|
password is the password for this login
|
|
|
|
base is the base DN where to perform the search, ie "ou=hr,o=GeezTeem"
|
|
|
|
attrs is a comma-separated list of attrs we will retrieve in the LDAP,
|
|
|
|
ie "uid,login"
|
|
|
|
filter is the query filter, ie "(&(attr1=Geez*)(status=OK))"
|
|
|
|
'''
|
2011-05-20 09:20:49 -05:00
|
|
|
def __init__(self):
|
|
|
|
# Get params from shell args.
|
2011-06-02 05:20:15 -05:00
|
|
|
if len(sys.argv) != 7:
|
2013-05-29 17:46:11 -05:00
|
|
|
print(LdapTester.__doc__)
|
2011-05-20 09:20:49 -05:00
|
|
|
sys.exit(0)
|
2011-06-02 05:20:15 -05:00
|
|
|
s = self
|
|
|
|
s.uri, s.login, s.password, s.base, s.attrs, s.filter = sys.argv[1:]
|
|
|
|
self.attrs = self.attrs.split(',')
|
2011-05-20 09:20:49 -05:00
|
|
|
self.tentatives = 5
|
|
|
|
self.timeout = 5
|
2011-06-02 05:20:15 -05:00
|
|
|
self.attrList = ['cfwbV2cn', 'logindisabled']
|
2011-05-20 09:20:49 -05:00
|
|
|
self.ssl = False
|
|
|
|
|
|
|
|
def test(self):
|
|
|
|
# Connect the the LDAP
|
2013-05-29 17:46:11 -05:00
|
|
|
print('Creating server object for server %s...' % self.uri)
|
2011-05-20 09:20:49 -05:00
|
|
|
server = ldap.initialize(self.uri)
|
2013-05-29 17:46:11 -05:00
|
|
|
print('Done. Login with %s...' % self.login)
|
2011-05-20 09:20:49 -05:00
|
|
|
server.simple_bind(self.login, self.password)
|
|
|
|
if self.ssl:
|
|
|
|
server.start_tls_s()
|
|
|
|
try:
|
|
|
|
for i in range(self.tentatives):
|
|
|
|
try:
|
2013-05-29 17:46:11 -05:00
|
|
|
print('Done. Performing a simple query on %s...'% self.base)
|
2011-05-20 09:20:49 -05:00
|
|
|
res = server.search_st(
|
2011-06-02 05:20:15 -05:00
|
|
|
self.base, ldap.SCOPE_ONELEVEL, filterstr=self.filter,
|
|
|
|
attrlist=self.attrs, timeout=5)
|
2013-05-29 17:46:11 -05:00
|
|
|
print('Got %d entries' % len(res))
|
2011-05-20 09:20:49 -05:00
|
|
|
break
|
|
|
|
except ldap.TIMEOUT:
|
2013-05-29 17:46:11 -05:00
|
|
|
print('Got timeout.')
|
2011-05-20 09:20:49 -05:00
|
|
|
except ldap.LDAPError, le:
|
2013-05-29 17:46:11 -05:00
|
|
|
print('%s %s' % (le.__class__.__name__, str(le)))
|
2011-05-20 09:20:49 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
if __name__ == '__main__':
|
|
|
|
LdapTester().test()
|
2011-06-02 05:20:15 -05:00
|
|
|
|
2011-05-20 09:20:49 -05:00
|
|
|
# ------------------------------------------------------------------------------
|