【问题标题】:Need help using python-ldap to query my LDAP server for user names需要帮助使用 python-ldap 查询我的 LDAP 服务器的用户名
【发布时间】: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


【解决方案1】:
def group_check(group, l):
    full_paths = []
    searchFilter = '(&(cn=*{}*))'.format(group)
    searchBase = 'OU=THISOU,DC=Company,DC=Domain,DC=com'
    restuls = l.search_s(searchBase, ldap.SCOPE_ONELEVEL, searchFilter, attrlist=['*'])
    try:
        if restuls:
            print 'Members Of {}\n'.format(group)
            members = restuls[0][1]['member']
            for mem in members:
                print '\t' + str(mem).split(',')[0].split('=')[1]
                full_paths.append(mem)

    except KeyError:
        print '\nNo Members? {}'.format(user)
    except Exception,e:
        print e

【讨论】:

    猜你喜欢
    • 2018-02-06
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多