Guest User

Untitled

a guest
Oct 17th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. ldapsearch -h hostname -b dc=ernet,dc=in -x "(&(uid=w2lame)(objectClass=posixAccount))" gidnumber
  2. ldapsearch -h hostname -b dc=ernet,dc=in -x "(&(gidNumber=1234)(objectClass=posixGroup))" cn
  3.  
  4. import ldap
  5. l = ldap.initialize('ldap://ldapserver')
  6. username = "uid=%s,ou=People,dc=mydotcom,dc=com" % username
  7. password = "my password"
  8. try:
  9. l.protocol_version = ldap.VERSION3
  10. l.simple_bind_s(username, password)
  11. valid = True
  12. except Exception, error:
  13. print error
  14.  
  15. l = ldap.initialize('ldap://ldap.myserver.com:389')
  16. binddn = "cn=myUserName,ou=GenericID,dc=my,dc=company,dc=com"
  17. pw = "myPassword"
  18. basedn = "ou=UserUnits,dc=my,dc=company,dc=com"
  19. searchFilter = "(&(gidNumber=123456)(objectClass=posixAccount))"
  20. searchAttribute = ["mail","department"]
  21. #this will scope the entire subtree under UserUnits
  22. searchScope = ldap.SCOPE_SUBTREE
  23. #Bind to the server
  24. try:
  25. l.protocol_version = ldap.VERSION3
  26. l.simple_bind_s(binddn, pw)
  27. except ldap.INVALID_CREDENTIALS:
  28. print "Your username or password is incorrect."
  29. sys.exit(0)
  30. except ldap.LDAPError, e:
  31. if type(e.message) == dict and e.message.has_key('desc'):
  32. print e.message['desc']
  33. else:
  34. print e
  35. sys.exit(0)
  36. try:
  37. ldap_result_id = l.search(basedn, searchScope, searchFilter, searchAttribute)
  38. result_set = []
  39. while 1:
  40. result_type, result_data = l.result(ldap_result_id, 0)
  41. if (result_data == []):
  42. break
  43. else:
  44. ## if you are expecting multiple results you can append them
  45. ## otherwise you can just wait until the initial result and break out
  46. if result_type == ldap.RES_SEARCH_ENTRY:
  47. result_set.append(result_data)
  48. print result_set
  49. except ldap.LDAPError, e:
  50. print e
  51. l.unbind_s()
  52.  
  53. def _ldap_list(ldap_server, base_dn, filter_, limit=0):
  54. """ Generator: get a list of search results from LDAP asynchronously. """
  55.  
  56. ldap_attributes = ["*"] # List of attributes that you want to fetch.
  57. result_id = ldap_server.search(base_dn, ldap.SCOPE_SUBTREE, filter_, ldap_attributes)
  58. records = 0
  59.  
  60. while 1:
  61. records += 1
  62.  
  63. if limit != 0 and records > limit:
  64. break
  65.  
  66. try:
  67. result_type, result_data = ldap_server.result(result_id, 0)
  68. except ldap.NO_SUCH_OBJECT:
  69. raise DirectoryError("Distinguished name (%s) does not exist." % base_dn)
  70.  
  71. if result_type == ldap.RES_SEARCH_ENTRY:
  72. dn = result_data[0][0]
  73. data = result_data[0][1]
  74. yield dn, data
  75. else:
  76. break
  77.  
  78. from commands import getoutput
  79. result = getoutput('ldapsearch -h hostname -b dc=ernet,dc=in -x "(&(uid=w2lame)(objectClass=posixAccount))"')
  80. print result
Add Comment
Please, Sign In to add comment