Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. (project1_env) [root@localhost python-ldap]# cat script1.py
  2.  
  3. import ldap
  4.  
  5. ## first you must open a connection to the server
  6. try:
  7. l = ldap.open("127.0.0.1")
  8. ## searching doesn't require a bind in LDAP V3. If you're using LDAP v2, set the next line appropriately
  9. ## and do a bind as shown in the above example.
  10. # you can also set this to ldap.VERSION2 if you're using a v2 directory
  11. # you should set the next option to ldap.VERSION2 if you're using a v2 directory
  12. l.protocol_version = ldap.VERSION3
  13. except ldap.LDAPError as e:
  14. print (e)
  15. # handle error however you like
  16.  
  17.  
  18. ## The next lines will also need to be changed to support your search requirements and directory
  19. baseDN = "ou=Users, dc=afik, dc=com"
  20. searchScope = ldap.SCOPE_SUBTREE
  21. ## retrieve all attributes - again adjust to your needs - see documentation for more options
  22. retrieveAttributes = None
  23. searchFilter = "cn=hr_user1"
  24.  
  25. try:
  26. ldap_result_id = l.search(baseDN, searchScope, searchFilter, retrieveAttributes)
  27. result_set = []
  28. while 1:
  29. result_type, result_data = l.result(ldap_result_id, 0)
  30. if (result_data == []):
  31. break
  32. else:
  33. ## here you don't have to append to a list
  34. ## you could do whatever you want with the individual entry
  35. ## The appending to list is just for illustration.
  36. if result_type == ldap.RES_SEARCH_ENTRY:
  37. result_set.append(result_data)
  38. print (result_set)
  39. except ldap.LDAPError as e:
  40. print (e)
  41.  
  42. (project1_env) [root@localhost python-ldap]# python script1.py
  43. Traceback (most recent call last):
  44. File "script1.py", line 9, in <module>
  45. l = ldap.open("127.0.0.1")
  46. AttributeError: module 'ldap' has no attribute 'open'
  47. (project1_env) [root@localhost python-ldap]#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement