📜 ⬆️ ⬇️

Retrieving information from Active Directory and Firebird

Good day to all. The task arose to obtain information from Active Direcoty (in particular, about the belonging of users to certain groups of rights) and the Firebird database. I would like to implement something like this in Python. But I can not imagine how. I rummaged through a bunch of materials, but I find it difficult to systematize them. It would be glad to provide assistance in this matter.

Possible Solution


The solution for obtaining information from Active Directory found here . Put modules for Python active_directory, win32api. But after performing the scripts it gives an error

UnicodeEncodeError: 'ascii' codec can't encode characters in position 10-18: ordinal not in range(128)

Something with the encoding, but I do not know how to solve the problem. Help!
')
There are more examples of using the ldap module, as advised by the respected ctrlok .



Here is a working example that receives from Active Directory the number of current users and their list

# -*- coding: cp1251 -*-
import codecs, sys
ctrl = 'domain_name'
outf = codecs.getwriter('cp1251')(sys.stdout, errors='replace')
sys.stdout = outf

import win32com.client

def get_all_users_in_domain():
conn = win32com.client.Dispatch("ADODB.Connection")
conn.Provider = "ADSDSOObject"
conn.Open("Active Directory Provider")
query = "SELECT ADsPath FROM 'LDAP://DC="+ctrl+"' WHERE objectClass='user' AND objectCategory='person'"
rs = conn.Execute(query)[0]
users = []
while not rs.EOF:
obj = win32com.client.GetObject(rs.Fields(0).Value)
users.append(obj)
rs.MoveNext()
conn.Close()
return (users)

def get_enabled_user_in_domain(users):
enabled_users = []
for x in range(len(users)):
if users[x].AccountDisabled:
enabled_users.append(users[x])
return (enabled_users)

def print_user (users):
print u" %d \n" %(len(users))
for x in range(0, len(users)):
print users[x].Name.replace(u'CN=','')

users = get_all_users_in_domain()
enabled_users = get_enabled_user_in_domain(users)
print_user(enabled_users)


Now the question is how to add groups to which each user belongs ...

Source: https://habr.com/ru/post/65096/


All Articles