import ldap

ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
conn = ldap.initialize('ldap://10.10.14.103')
# conn.simple_bind_s('admin@admin', '3pro01003ton')


groups = []
results = conn.search_s('dc=EPP,dc=RU', ldap.SCOPE_SUBTREE, 'objectClass=posixGroup', ["cn", "memberUid"])
for result in results:
    print(result[1])
    groups.append(result[1]['cn'][0].decode('utf-8'))

print(groups)

# for group in groups:
#     group_name = group
#     group_users = conn.search_s('dc=EPP,dc=RU', ldap.SCOPE_BASE, "(objectClass=posixGroup)", ["*"])
#     print(group_users)
#     user_list = []
#     for user in group_users[0][1]["memberUid"]:
#         user_dn = "uid={},ou=users,dc=EPP,dc=RU".format(user.decode())
#         user_data = conn.search_s(user_dn, ldap.SCOPE_BASE)
#         user_list.append(user_data[0][1]["cn"][0])
#     print("Group {} contains users: {}".format(group_name, ", ".join(user_list)))