2011-05-20 09:20:49 -05:00
|
|
|
'''This script allows to check a LDAP connection.'''
|
2013-09-09 16:14:50 -05:00
|
|
|
import sys
|
2014-10-07 06:14:16 -05:00
|
|
|
from appy.shared.ldap_connector import LdapConnector
|
2011-05-20 09:20:49 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class LdapTester:
|
2013-09-09 16:14:50 -05:00
|
|
|
'''Usage: python checkldap.py ldapUri login password base attrs filter scope
|
2011-06-02 05:20:15 -05:00
|
|
|
|
|
|
|
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))"
|
2013-09-05 03:42:19 -05:00
|
|
|
scope is the scope of the search, and can be:
|
|
|
|
BASE To search the object itself on base
|
|
|
|
ONELEVEL To search base's immediate children
|
|
|
|
SUBTREE To search base and all its descendants
|
2011-06-02 05:20:15 -05:00
|
|
|
'''
|
2011-05-20 09:20:49 -05:00
|
|
|
def __init__(self):
|
|
|
|
# Get params from shell args.
|
2013-09-05 03:42:19 -05:00
|
|
|
if len(sys.argv) != 8:
|
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
|
2013-09-05 03:42:19 -05:00
|
|
|
s.uri,s.login,s.password,s.base,s.attrs,s.filter,s.scope = sys.argv[1:]
|
2011-06-02 05:20:15 -05:00
|
|
|
self.attrs = self.attrs.split(',')
|
2011-05-20 09:20:49 -05:00
|
|
|
self.tentatives = 5
|
|
|
|
self.timeout = 5
|
2013-09-09 16:14:50 -05:00
|
|
|
self.attributes = ['cn']
|
2011-05-20 09:20:49 -05:00
|
|
|
self.ssl = False
|
|
|
|
|
|
|
|
def test(self):
|
|
|
|
# Connect the the LDAP
|
2013-09-09 16:14:50 -05:00
|
|
|
print('Connecting to... %s' % self.uri)
|
|
|
|
connector = LdapConnector(self.uri)
|
|
|
|
success, msg = connector.connect(self.login, self.password)
|
|
|
|
if not success: return
|
|
|
|
# Perform the query.
|
|
|
|
print ('Querying %s...' % self.base)
|
|
|
|
res = connector.search(self.base, self.scope, self.filter,
|
|
|
|
self.attributes)
|
|
|
|
print('Got %d results' % len(res))
|
2011-05-20 09:20:49 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2013-09-09 16:14:50 -05:00
|
|
|
if __name__ == '__main__': LdapTester().test()
|
2011-05-20 09:20:49 -05:00
|
|
|
# ------------------------------------------------------------------------------
|