apache_ldap.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from ldap3 import ALL, ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES, Connection, Server
  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(
  11. "ou=cognos,dc=ibm,dc=com",
  12. "(objectclass=person)",
  13. "SUBTREE",
  14. attributes=[ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES],
  15. )
  16. format_string = "{:15} {:25} {:19} {:25} {}"
  17. for e in conn.entries:
  18. desc = ""
  19. email = ""
  20. if "description" in e:
  21. desc = e.description
  22. if "mail" in e:
  23. email = e.mail
  24. # print(e.entry_to_json())
  25. print(format_string.format(str(e.uid), str(e.givenName), str(e.createTimestamp)[:19], str(email), desc))
  26. else:
  27. print(conn.result)
  28. if __name__ == "__main__":
  29. # connect_pyldap()
  30. connect_ldap3()
  31. # from ldap3 import Server, Connection, AUTH_SIMPLE, STRATEGY_SYNC, ALL
  32. # s = Server(HOST, port=389, get_info=ALL)
  33. # c = Connection(s, authentication=AUTH_SIMPLE, user=user_dn, password=PASSWORD, check_names=True,
  34. # lazy=False, client_strategy=STRATEGY_SYNC, raise_exceptions=True)
  35. # c.open()
  36. # c.bind()