【发布时间】:2018-02-05 18:10:45
【问题描述】:
我希望我的脚本只查询 LDAP 并为我提供属于我在 SAMPLE_groupName 变量中指定的任何组的所有用户的列表。
这是我的脚本
server = 'ldap://myserver'
dn = 'uid=jonny,cn=users,cn=accounts,dc=example,dc=com'
base = 'cn=coolGuys,cn=groups,cn=accounts,dc=example,dc=com'
pw = "password"
filter = '(objectclass=*)'
attrs = ['member']
con = ldap.initialize(server)
try:
con.start_tls_s()
con.simple_bind_s(dn,pw)
print con.search_s(base, ldap.SCOPE_SUBTREE, filter, attrs)
except ldap.INVALID_CREDENTIALS:
print "Your username or password is incorrect."
sys.exit()
except ldap.LDAPError, e:
if type(e.message) == dict and e.message.has_key('desc'):
print e.message['desc']
else:
print e
sys.exit()
finally:
print "unbinding."
con.unbind()
这是输出
[('cn=coolGuys,cn=groups,cn=accounts,dc=example,dc=com', {'member',['uid=jonny,cn=users,cn=accounts,dc=openstack,dc=local']})]
输出显示一个成员在coolGuys 组中,这在我的情况下是正确的。所以这是我的问题... 我怎样才能让输出只是“jonny”而不是我上面的那长串输出?
【问题讨论】:
-
尝试使用具有适当权限以至少读取所有条目的凭据进行绑定。
-
是的,我正在使用适当的凭据进行绑定。只是不知道如何让它简单地返回一个用户名。还要记住,这是在 Linux 机器上运行的 openLdap
-
使用 python-ldap 我会执行搜索 'restuls = l.search_s(searchBase, ldap.SCOPE_ONELEVEL, searchFilter, attrlist=['*'])' 然后获取成员 'members = restuls[ 0][1]['member']' 示例作为下面的答案
标签: ldap openldap ldap-query python-ldap