Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from ldap3 import Server, Connection, ALL, NTLM, SUBTREE, ALL_ATTRIBUTES
- import sys
- import json
- managers_name = 'Moshe Ben Ezra'
- first_name = "Adolf"
- last_name = "Hitler"
- first_last = "{} {}".format(first_name, last_name)
- first_last_dot = "{}.{}".format(first_name, last_name)
- title = "Fuhrer"
- phone_number = "054-6985215"
- employee_id = 1234
- # Create LDAP connection
- server = Server('ldaps://dc01.corp.naturalint.com:636', connect_timeout=5)
- conn = Connection(server=server, user="corp.naturalint.com\\evyatar.tzik", password="evyacik@204", authentication=NTLM)
- if conn.bind():
- print ('Connection to LDAP - successfull!')
- else:
- print('Connection to LDAP - BAD :(')
- sys.exit(1)
- # Get manager's DN
- conn.search(search_base = 'OU=Departments,OU=Natural Intelligence,DC=corp,DC=naturalint,DC=com',
- search_filter = '(cn={})'.format(managers_name),
- search_scope = SUBTREE,
- attributes = None,
- paged_size = 5)
- num_of_mgrs = len(conn.response)
- if num_of_mgrs > 1 or num_of_mgrs == 0:
- print('Got invalid number of manager(s): {}, exiting!'.format(num_of_mgrs))
- sys.exit(1)
- # print(conn.response_to_json())
- managers_dn = conn.response[0]['dn']
- base_dn = ','.join(managers_dn.split(',')[1:])
- user_full_dn = 'cn={first_name} {last_name},{base_dn}'.format(first_name=first_name, last_name=last_name, base_dn=base_dn)
- # perform the Add operation
- creation_result = conn.add(
- user_full_dn,
- ['organizationalPerson', 'person'],
- {
- "enabled": True,
- 'givenName': first_name,
- 'sn': last_name,
- 'gidNumber': 0,
- "samAccountName": first_last_dot,
- "userPrincipalName": "{}@corp.naturalint.com".format(first_last_dot),
- "mail": "{}@naturalint.con".format(first_last_dot),
- "displayName": first_last,
- "employeeID": employee_id
- }
- )
- print(creation_result)
- print(conn.result)
- print(conn.response)
- sys.exit(0)
- # Set password and enable user
- conn.extend.microsoft.modify_password(user_full_dn, "Natural1$")
- conn.modify(user_full_dn, {'userAccountControl': [('MODIFY_REPLACE', 512)]})
- print(conn.result)
- conn.unbind()
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement