需要帮助使用 python-ldap 查询我的 LDAP 服务器的用户名
Need help using python-ldap to query my LDAP server for user names
我希望我的脚本简单地查询 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" 而不是我上面的那一长串输出?
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
我希望我的脚本简单地查询 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" 而不是我上面的那一长串输出?
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