Пытаюсь получить список всех sAMAccountName
через ldap3 из active directory. Пока получается лишь вывести список всех пользователей у кого он есть.
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import ldap3
from pprint import pprint
ad_name='DC=D111TEST,DC=rdi'
server_win_uri = '192.168.1.240'
search_filter = "(&(objectClass=person)(sAMAccountName=*)(sn=*))"
win_bind_name = "Администратор@D111TEST.rdi"
win_bind_passwd = "password"
attrs = ['*']
def get_users_win_data(ip,search_base,search_filter,attrs,win_bind_name,win_bind_passwd):
server = ldap3.Server('ldap://{}'.format(ip))
with ldap3.Connection(server,user=win_bind_name,password=win_bind_passwd) as conn:
conn.search(search_base, search_filter, attributes=attrs)
return(conn.entries)
data2=get_users_win_data(server_win_uri,ad_name,search_filter,attrs,win_bind_name,win_bind_passwd)
pprint(data2)
На выходе я получаю полный список, а нужно только список всех sAMAccountName
.
[DN: CN=someuser,OU=Masters,OU=lab2,DC=D111TEST,DC=rdi - STATUS: Read - READ TIME: 2018-04-27T14:08:06.749917
accountExpires: 9999-12-31 23:59:59.999999
badPasswordTime: 2018-04-24 11:56:12.762737+00:00
badPwdCount: 0
cn: someuser
codePage: 0
countryCode: 0
dSCorePropagationData: 1601-01-01 00:00:00+00:00
departmentNumber: ou=Masters
distinguishedName: CN=someuser,OU=Masters,OU=lab2,DC=D111TEST,DC=rdi
givenName: someuser
instanceType: 4
lastLogoff: 1601-01-01 00:00:00+00:00
lastLogon: 2018-04-24 11:56:18.622112+00:00
lastLogonTimestamp: 2018-04-24 10:02:28.178299+00:00
logonCount: 4
name: someuser
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=D111TEST,DC=rdi
objectClass: top
person
organizationalPerson
user
inetOrgPerson
objectGUID: {409fcaa2-7f38-4323-85b8-73f3b502bdd7}
objectSid: S-1-5-21-1391473009-3346311289-2259281666-1128
primaryGroupID: 513
pwdLastSet: 2018-04-24 12:04:53.419655+00:00
sAMAccountName: someuser
sAMAccountType: 805306368
sn: someuser
telephoneNumber: 1111
uSNChanged: 127091
uSNCreated: 118937
userAccountControl: 544
whenChanged: 2018-04-24 12:04:53+00:00
whenCreated: 2018-04-24 07:14:22+00:00
,
DN: CN=b.young,OU=lab1,DC=D111TEST,DC=rdi - STATUS: Read - READ TIME: 2018-04-27T14:08:06.752707
accountExpires: 9999-12-31 23:59:59.999999
badPasswordTime: 1601-01-01 00:00:00+00:00
badPwdCount: 0
cn: b.young
codePage: 0
countryCode: 0
dSCorePropagationData: 1601-01-01 00:00:00+00:00
departmentNumber: DEV
distinguishedName: CN=b.young,OU=lab1,DC=D111TEST,DC=rdi
givenName: Beatrix
instanceType: 4
lastLogoff: 1601-01-01 00:00:00+00:00
lastLogon: 1601-01-01 00:00:00+00:00
logonCount: 0
name: b.young
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=D111TEST,DC=rdi
objectClass: top
person
organizationalPerson
user
inetOrgPerson
objectGUID: {964ccd96-96c6-48bb-8540-c76168862262}
objectSid: S-1-5-21-1391473009-3346311289-2259281666-1122
primaryGroupID: 513
pwdLastSet: 1601-01-01 00:00:00+00:00
sAMAccountName: $231000-QQ3FD6G7KIUJ
sAMAccountType: 805306368
sn: Young
telephoneNumber: 1111
uSNChanged: 114806
uSNCreated: 114805
userAccountControl: 546
whenChanged: 2018-04-23 09:49:40+00:00
whenCreated: 2018-04-23 09:49:40+00:00
]
attrs = ['sAMAccountName']
?ldap3.core.exceptions.LDAPAttributeError: invalid attribute type sAMAccountName
attrs = ['distinguishedName']
то работает, не работает толькоattrs = ['sAMAccountName']