python可删除用户程序,python-ldap和Microsoft Active Directory:连接并删除用户

python-ldap newb here. I am trying to do this with the following sample code:

import ldap

## first you must bind so we're doing a simple bind first

try:

l = ldap.open("valid ip")

l.set_option(ldap.OPT_REFERRALS, 0)

l.protocol_version = ldap.VERSION3

# Pass in a valid username and password to get

# privileged directory access.

# If you leave them as empty strings or pass an invalid value

# you will still bind to the server but with limited privileges.

username = "cn=administrator, o=joe.local"

password = "password"

# Any errors will throw an ldap.LDAPError exception

# or related exception so you can ignore the result

l.simple_bind(username, password)

except ldap.LDAPError, e:

print e

# handle error however you like

# The next lines will also need to be changed to support your requirements and directory

deleteDN = "uid=hihihi, ou=LoginUsers,o=joe.local"

try:

# you can safely ignore the results returned as an exception

# will be raised if the delete doesn't work.

l.delete_s(deleteDN)

except ldap.LDAPError, e:

print e

## handle error however you like

I get various errors:

Using IP of VM:

{'info': '000004DC: LdapErr: DSID-0C0909A2, comment: In order to perform this op

eration a successful bind must be completed on the connection., data 0, v1db1',

'desc': 'Operations error'}

Using localhost or 127.0.0.1 :

{'desc': "Can't contact LDAP server"}

{'desc': "Can't contact LDAP server"}

I have looked at the following S.O. posts with no resolution:

解决方案

According to the documentation, ldap.open is deprecated. You should try ldap.initialize, like the two links you provided. Also, make sure there are no spaces in your distinguished names: "cn=administrator, o=joe.local".

If that doesn't fix the problem, then make sure to mention which line that error is coming from.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值