2023年6月21日发(作者:)

python3操作openldap##pip3.7 install python-ldap==3.3.1import ldapfrom ldap import modlistldapconn = lize('ldap://192.168.3.99:389',bytes_mode=False) ###python3⼀定要加上bytes_mode=False 这个参数,否则会报错_bind('cn=admin,dc=xthktech,dc=cn','xxxxx')#验证⽤户密码try: _bind_s('uid=0000,ou=people,dc=xthktech,dc=cn','xxx') print('登录成功')except D_CREDENTIALS as e: print('⽤户名或密码错误')#修改⽤户密码try: res = _s('uid=test,ou=people,dc=xthktech,dc=cn','xxxx','44') print('密码更改成功')except ING_TO_PERFORM as e: print('旧密码验证错误')#搜索⽤户信息searchScope = _SUBTREEsearchFilter = 'uid=0000'base_dn = 'ou=people,dc=xthktech,dc=cn'res = _s(base_dn, searchScope, searchFilter, None)if len(res) == 0: print('not found')else: print(res)#修改⽤户信息old = {'mail':[b'ffff@'],'sn':[b'test']}new = {'mail':[b'loveyou@'],'sn':[b'fuckyou']}ldif =Modlist(old,new)_s('uid=test,ou=people,dc=xthktech,dc=cn',ldif)_s()#新增⽤户try: attrs = {} attrs['objectclass'] = [b'inetOrgPerson'] attrs['cn'] = [b'zhang san'] attrs['uid'] = [b'test'] attrs['sn'] = [b'xx'] attrs['telephoneNumber'] = [b'1'] attrs['mail'] = [b'ff@'] attrs['userPassword'] = [] ldif = list(attrs) s=_s('uid=test,ou=people,dc=xthktech,dc=cn',ldif) print(s) print('ok')except Y_EXISTS as e: print('数据库已经存在该⽤户')try: _s('uid=xxxx,ou=people,dc=xthktech,dc=cn',None,'123456')except Exception as e: print(e)_s()#删除⽤户try: _s('uid=888,ou=people,dc=xthktech,dc=cn') print('删除⽤户成功')except _SUCH_OBJECT as e: print('数据库中没有找到该⽤户')

2023年6月21日发(作者:)

python3操作openldap##pip3.7 install python-ldap==3.3.1import ldapfrom ldap import modlistldapconn = lize('ldap://192.168.3.99:389',bytes_mode=False) ###python3⼀定要加上bytes_mode=False 这个参数,否则会报错_bind('cn=admin,dc=xthktech,dc=cn','xxxxx')#验证⽤户密码try: _bind_s('uid=0000,ou=people,dc=xthktech,dc=cn','xxx') print('登录成功')except D_CREDENTIALS as e: print('⽤户名或密码错误')#修改⽤户密码try: res = _s('uid=test,ou=people,dc=xthktech,dc=cn','xxxx','44') print('密码更改成功')except ING_TO_PERFORM as e: print('旧密码验证错误')#搜索⽤户信息searchScope = _SUBTREEsearchFilter = 'uid=0000'base_dn = 'ou=people,dc=xthktech,dc=cn'res = _s(base_dn, searchScope, searchFilter, None)if len(res) == 0: print('not found')else: print(res)#修改⽤户信息old = {'mail':[b'ffff@'],'sn':[b'test']}new = {'mail':[b'loveyou@'],'sn':[b'fuckyou']}ldif =Modlist(old,new)_s('uid=test,ou=people,dc=xthktech,dc=cn',ldif)_s()#新增⽤户try: attrs = {} attrs['objectclass'] = [b'inetOrgPerson'] attrs['cn'] = [b'zhang san'] attrs['uid'] = [b'test'] attrs['sn'] = [b'xx'] attrs['telephoneNumber'] = [b'1'] attrs['mail'] = [b'ff@'] attrs['userPassword'] = [] ldif = list(attrs) s=_s('uid=test,ou=people,dc=xthktech,dc=cn',ldif) print(s) print('ok')except Y_EXISTS as e: print('数据库已经存在该⽤户')try: _s('uid=xxxx,ou=people,dc=xthktech,dc=cn',None,'123456')except Exception as e: print(e)_s()#删除⽤户try: _s('uid=888,ou=people,dc=xthktech,dc=cn') print('删除⽤户成功')except _SUCH_OBJECT as e: print('数据库中没有找到该⽤户')