我试图使用Python模块在LDAP输入中查找/搜索最大UID值。我的代码看起来像这样
def search_max_uid():
filter_uid = 'uid=*'
attributes = ['uidNumber']
resulting = l.search_ext(base_dn,ldap.SCOPE_SUBTREE,filter_uid,attributes)
print resulting
从整个服务器中获得最大UID后,我可以 1并将新用户添加到组中。我看到了一些帖子,例如http://www.openldap.org/lists/openldap-software/200110/msg00539.html和http://www.perlmonks.org/?node_id = 457108,这与我的问题非常相似
有人可以帮助我找到最大uid,以便我可以解决这个问题。
试图解决类似的东西,我认为这有助于获取下一个可用的uidnumber:
import ldap
l = ldap.initialize("ldap://localhost")
l.simple_bind_s("cn=blah,dc=blah,dc=blah", supersecretpassword)
res = l.search_s("dc=blah,dc=blah", ldap.SCOPE_SUBTREE, 'objectclass=posixaccount', ['uidNumber'])
uidNum = 0
for a in res:
uidNumtemp = a[1].get('uidNumber')[0]
if uidNumtemp > uidNum:
uidNum = uidNumtemp
print "Highest:", uidNum
nextNum = int(uidNum) + 1
print "next uidNumber:", nextNum
如果使用支持服务器端排序的LDAP服务器(请参阅RFC 2891(,例如带有Slapo-SSSSVLV的OpenLDAP,您可以通过搜索最高的数量来搜索最高的数量排序顺序。
基于Python-dap的Python片段(摘自æ-dir的CLI工具之一(:
import ldap
from ldap.controls.sss import SSSRequestControl
def highest_id(ldap_conn, searchbase, id_attr):
"""
search the highest value of `id_attr' by using server-side (reverse) sorting
"""
# reverse sorting request control
sss_control = SSSRequestControl(criticality=True, ordering_rules=['-'+id_attr])
# send search request
msg_id = ldap_conn.search(
searchbase,
ldap.SCOPE_SUBTREE,
'({0}=*)'.format(id_attr),
attrlist=[id_attr],
sizelimit=1,
serverctrls=[sss_control],
)
# collect result
ldap_result = []
try:
for _, res_data, _, res_controls in ldap_conn.results(
msg_id,
add_ctrls=0
):
ldap_result.extend(res_data)
except ldap.SIZELIMIT_EXCEEDED:
pass
if not ldap_result:
logging.error('No entry with attribute %r found!', id_attr)
raise ValueError('No LDAP result!')
highest_id_number = int(ldap_result[0][1][id_attr][0])
logging.debug('Highest %r value found: %d', id_attr, highest_id_number)
return highest_id_number
请注意,这并不总是您在分配新ID时想要的,因为ID号空间中的间隙未使用(重新(。
还确保使用服务器端独特的约束插件,例如OpenLDAP的叠加slapo-Inique。这避免了重复,以防并发客户添加新条目。