|
| 1 | +import ldap3 |
| 2 | +from ldap3 import Server, Connection |
| 3 | +from ldap3.extend.microsoft.addMembersToGroups import ad_add_members_to_groups as addUsersInGroups |
| 4 | +import sys |
| 5 | + |
| 6 | + |
| 7 | +class AdDetails: |
| 8 | + ipaddress = "ip-address of AD server" |
| 9 | + domain = 'your domain' |
| 10 | + searchbase = 'DC=" ", DC=" " ' |
| 11 | + username = input("Please enter your username: ") |
| 12 | + password = input("Please enter your password: ") |
| 13 | + adgroup = input("Enter Active directory group: ") |
| 14 | + |
| 15 | + |
| 16 | +server = Server(AdDetails.ipaddress, get_info=ldap3.ALL) |
| 17 | +try: |
| 18 | + conn = Connection(server, AdDetails.username, AdDetails.password, auto_bind=True) |
| 19 | + print("*" * 30) |
| 20 | + print("Ldap connected \n") |
| 21 | + |
| 22 | + |
| 23 | +except: |
| 24 | + |
| 25 | + print("*" * 30) |
| 26 | + print('LDAP Bind Failed: ') |
| 27 | + print("\n") |
| 28 | + print("Exit...................") |
| 29 | + sys.exit(1) |
| 30 | + |
| 31 | + |
| 32 | +def group(): |
| 33 | + conn.search(search_base=AdDetails.searchbase, search_filter='(objectclass=group)', attributes=[ldap3.ALL_ATTRIBUTES, |
| 34 | + ldap3.ALL_OPERATIONAL_ATTRIBUTES]) |
| 35 | + |
| 36 | + for entry in conn.entries: |
| 37 | + gname = entry.name |
| 38 | + groups = entry.distinguishedName |
| 39 | + if AdDetails.adgroup in gname: |
| 40 | + return groups |
| 41 | + else: |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +def error(): |
| 46 | + if group() is None: |
| 47 | + print(f"An error occurred. Group {AdDetails.adgroup} does not exist. ") |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + |
| 51 | +def user(): |
| 52 | + error() |
| 53 | + conn.search(search_base=AdDetails.searchbase, search_filter='(objectclass=person)', |
| 54 | + attributes=[ldap3.ALL_ATTRIBUTES, |
| 55 | + ldap3.ALL_OPERATIONAL_ATTRIBUTES]) |
| 56 | + |
| 57 | + with open('users') as f: |
| 58 | + users_list = f.read().splitlines() |
| 59 | + for entry in conn.entries: |
| 60 | + result = entry.name |
| 61 | + try: |
| 62 | + get = entry.memberOf |
| 63 | + except: |
| 64 | + get = (entry.name, " No group") |
| 65 | + pass |
| 66 | + for u in users_list: |
| 67 | + if u in result: |
| 68 | + if group() in get: |
| 69 | + print("*" * 80) |
| 70 | + print(f"\n{u} is already member of {group()} \n") |
| 71 | + print("Skipped nothing to do ") |
| 72 | + elif group() not in get: |
| 73 | + dname = entry.distinguishedName |
| 74 | + print("*" * 80) |
| 75 | + print(f"\n{u} not in group adding user to the {group()} \n") |
| 76 | + groups_dn = str(group()) |
| 77 | + members_dn = str(dname) |
| 78 | + addUsersInGroups(conn, members_dn, groups_dn, raise_error="failed", fix="user in group") |
| 79 | + print(f"{u} have been added to the group ") |
| 80 | + print("*" * 80) |
| 81 | + print("\n") |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + AdDetails |
| 86 | + group() |
| 87 | + user() |
| 88 | + print("\n") |
| 89 | + print("Exit...................") |
0 commit comments