Search code examples
pythonldapldap-query

LDAP query in python


I want to execute the following query in the ldap

ldapsearch -h hostname -b dc=ernet,dc=in -x "(&(uid=w2lame)(objectClass=posixAccount))" gidnumber
ldapsearch -h hostname -b dc=ernet,dc=in -x "(&(gidNumber=1234)(objectClass=posixGroup))" cn

And use the variables thus obtained. How can I do that?


Solution

  • You probably want to use the ldap module. Code would look something like:

    import ldap
    l = ldap.initialize('ldap://ldapserver')
    username = "uid=%s,ou=People,dc=mydotcom,dc=com" % username
    password = "my password"
    try:
        l.protocol_version = ldap.VERSION3
        l.simple_bind_s(username, password)
        valid = True
    except Exception, error:
        print error