Last active
August 13, 2019 06:49
-
-
Save jossef/a263d8634f42e99966ffd55e16bdf4e2 to your computer and use it in GitHub Desktop.
Active Directory create test users and groups, link them to the groups
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ldap3 | |
| from ldap3 import Server, Connection | |
| from ldap3.extend.microsoft.addMembersToGroups import ad_add_members_to_groups | |
| def domain_to_distinguished_name(domain): | |
| return ','.join(map(lambda x: 'dc=' + x, domain.split('.'))) | |
| def main(): | |
| domain = 'my.domain.com' | |
| base_dn = domain_to_distinguished_name(domain) | |
| username = '...' | |
| password = '...' | |
| host = '192.168....' | |
| # ----------------------- | |
| server = Server(host, get_info=ldap3.ALL) | |
| session = Connection(server, | |
| version=3, | |
| auto_bind=True, | |
| raise_exceptions=False, | |
| check_names=False, | |
| user=domain + '\\' + username, password=password, authentication=ldap3.NTLM) | |
| session.bind() | |
| test_data_ou = 'ou=test-data' | |
| test_data_dn = ','.join([test_data_ou, base_dn]) | |
| session.add(test_data_dn, 'organizationalUnit') | |
| groups_count = 3 | |
| users_count = 2000 | |
| users_dn = [] | |
| groups_dn = [] | |
| for i in range(1, groups_count + 1): | |
| group_cn = 'cn=Test Group {}'.format(str(i).zfill(5)) | |
| group_dn = ','.join([group_cn, test_data_dn]) | |
| session.add(group_dn, 'group') | |
| groups_dn.append(group_dn) | |
| for i in range(1, users_count + 1): | |
| user_id = str(i).zfill(5) | |
| user_full_name = 'Test User {}'.format(user_id) | |
| user_cn = 'cn={}'.format(user_full_name) | |
| country_ou = 'ou=IL' | |
| country_dn = ','.join([country_ou, test_data_dn]) | |
| session.add(country_dn, 'organizationalUnit') | |
| user_dn = ','.join([user_cn, country_dn]) | |
| session.add(user_dn, 'user', | |
| {'givenName': user_full_name, | |
| 'sn': user_id, | |
| 'departmentNumber': 'DEV', | |
| 'telephoneNumber': '+9725000{}'.format(user_id)}) | |
| users_dn.append(user_dn) | |
| ad_add_members_to_groups(session, users_dn, groups_dn) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment