Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. import ldap3
  2.  
  3. conn = ldap3.Connection(
  4.     server="ldaps://foobar",
  5.     user="username@domain",  # normally full DN, but AD supports this format as well
  6.     password="password",
  7.     auto_bind=ldap3.AUTO_BIND_NONE,
  8.     authentication=ldap3.SIMPLE,
  9.     raise_exceptions=True,
  10.     auto_referrals=False,  # 90% you want it set to False
  11.     receive_timeout=10,  # seconds, exception afterwards
  12. )
  13.  
  14. conn.start_tls()
  15. conn.bind()
  16.  
  17. search = conn.extend.standard.paged_search(
  18.     search_base="dc=domain",
  19.     search_filter="(userPrincipalName=username@domain)",  # or (cn=username) or (sAMAccountName=username) or whatever
  20.     search_scope=ldap3.SUBTREE,
  21.     attributes=ldap3.ALL_ATTRIBUTES,
  22.     dereference_aliases=ldap3.DEREF_NEVER,
  23.     generator=True,
  24. )
  25.  
  26. entries = [entry for entry in search if entry["type"] == "searchResEntry"]  # not sure how to get rid of all the aliases otherwise
  27.  
  28. assert len(entries) is 1, "got {0} entries".format(len(entries))
  29. entry = entries[0]
  30.  
  31. dn = entry["dn"]
  32.  
  33. changes = {
  34.     "attributeName": [
  35.         [ldap3.MODIFY_DELETE, ["old value 1", "old value 2",]],
  36.         [ldap3.MODIFY_ADD, ["a new value"]],
  37.     ]
  38. }
  39.  
  40. conn.modify(dn, changes)
  41.  
  42. conn.unbind()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement