apache_ldap.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from ldap3 import Server, Connection, ALL, ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES
  2. from ldap3.core.exceptions import LDAPCursorError
  3. def connect_ldap3():
  4. server = Server('localhost:10389', get_info=ALL, use_ssl=False)
  5. # conn = Connection(server, 'uid=admin,ou=system', 'gc01gapsC$', auto_bind='TLS_AFTER_BIND') # 'uid=admin,ou=system'
  6. # status = conn.search('ou=cognos,dc=ibm,dc=com', '(objectclass=person)', 'SUBTREE')
  7. # print(conn.entries)
  8. conn = Connection(server, user='uid=Global1,ou=cognos,dc=ibm,dc=com', password='Cognos#11')
  9. if conn.bind():
  10. conn.search('ou=cognos,dc=ibm,dc=com', '(objectclass=person)', 'SUBTREE', attributes=[ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES])
  11. format_string = '{:15} {:25} {:19} {:25} {}'
  12. for e in conn.entries:
  13. desc = ""
  14. email = ""
  15. if 'description' in e:
  16. desc = e.description
  17. if 'mail' in e:
  18. email = e.mail
  19. # print(e.entry_to_json())
  20. print(format_string.format(str(e.uid), str(e.givenName), str(e.createTimestamp)[:19], str(email), desc))
  21. else:
  22. print(conn.result)
  23. if __name__ == '__main__':
  24. # connect_pyldap()
  25. connect_ldap3()
  26. # from ldap3 import Server, Connection, AUTH_SIMPLE, STRATEGY_SYNC, ALL
  27. # s = Server(HOST, port=389, get_info=ALL)
  28. # c = Connection(s, authentication=AUTH_SIMPLE, user=user_dn, password=PASSWORD, check_names=True, lazy=False, client_strategy=STRATEGY_SYNC, raise_exceptions=True)
  29. # c.open()
  30. # c.bind()