Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from ldap3 import Server, Connection, ALL, NTLM, SUBTREE, ALL_ATTRIBUTES
  3. import sys
  4. import json
  5.  
  6. managers_name = 'Moshe Ben Ezra'
  7. first_name = "Adolf"
  8. last_name = "Hitler"
  9. first_last = "{} {}".format(first_name, last_name)
  10. first_last_dot = "{}.{}".format(first_name, last_name)
  11. title = "Fuhrer"
  12. phone_number = "054-6985215"
  13. employee_id = 1234
  14.  
  15. # Create LDAP connection
  16. server = Server('ldaps://dc01.corp.naturalint.com:636', connect_timeout=5)
  17. conn = Connection(server=server, user="corp.naturalint.com\\evyatar.tzik", password="evyacik@204", authentication=NTLM)
  18. if conn.bind():
  19.     print ('Connection to LDAP - successfull!')
  20. else:
  21.     print('Connection to LDAP - BAD :(')
  22.     sys.exit(1)
  23.  
  24. # Get manager's DN
  25. conn.search(search_base = 'OU=Departments,OU=Natural Intelligence,DC=corp,DC=naturalint,DC=com',
  26.          search_filter = '(cn={})'.format(managers_name),
  27.          search_scope = SUBTREE,
  28.          attributes = None,
  29.          paged_size = 5)
  30.  
  31. num_of_mgrs = len(conn.response)
  32.  
  33. if num_of_mgrs > 1 or num_of_mgrs == 0:
  34.     print('Got invalid number of manager(s): {}, exiting!'.format(num_of_mgrs))
  35.     sys.exit(1)
  36.  
  37. # print(conn.response_to_json())
  38. managers_dn = conn.response[0]['dn']
  39. base_dn = ','.join(managers_dn.split(',')[1:])
  40. user_full_dn = 'cn={first_name} {last_name},{base_dn}'.format(first_name=first_name, last_name=last_name, base_dn=base_dn)
  41.  
  42. # perform the Add operation
  43. creation_result = conn.add(
  44.     user_full_dn,
  45.     ['organizationalPerson', 'person'],
  46.     {
  47.         "enabled": True,
  48.         'givenName': first_name,
  49.         'sn': last_name,
  50.         'gidNumber': 0,
  51.         "samAccountName": first_last_dot,
  52.         "userPrincipalName": "{}@corp.naturalint.com".format(first_last_dot),
  53.         "mail": "{}@naturalint.con".format(first_last_dot),
  54.         "displayName": first_last,
  55.         "employeeID": employee_id
  56.     }
  57. )
  58. print(creation_result)
  59. print(conn.result)
  60. print(conn.response)
  61. sys.exit(0)
  62. # Set password and enable user
  63. conn.extend.microsoft.modify_password(user_full_dn, "Natural1$")
  64. conn.modify(user_full_dn, {'userAccountControl': [('MODIFY_REPLACE', 512)]})
  65.  
  66.  
  67. print(conn.result)
  68.  
  69. conn.unbind()
  70.  
  71.  
  72. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement