Python-ldap 修改电话号码

3

我希望使用Python(-ldap)脚本更改AD中的移动电话号码。

这是我尝试使用的代码:

# import needed modules
import ldap
import ldap.modlist as modlist

# Open a connection
l = ldap.initialize("ldap://host/",trace_level=3)

# Bind/authenticate with a user with apropriate rights to add objects
l.simple_bind_s("user@domain","pw")

# The dn of our existing entry/object
dn="CN=common name,OU=Users,OU=x,DC=y,DC=z,DC=e"

# Some place-holders for old and new values
name = 'mobile'
nr1 = '+4712781271232'
nr2 = '+9812391282822'

old = {name:nr1}
new = {name:nr2}

# Convert place-holders for modify-operation using modlist-module
ldif = modlist.modifyModlist(old,new)

# Do the actual modification
l.modify_s(dn, ldif)

# Its nice to the server to disconnect and free resources when done
l.unbind_s()

很不幸,我遇到了以下错误:

ldap.UNWILLING_TO_PERFORM: {'info': u'00000057: LdapErr: DSID-0C090FC7, comment: Error in attribute conversion operation, data 0, v4563', 'desc': u'Server is unwilling to perform'}

我可以通过将旧条目留空来删除该条目,但是当我尝试设置它时,我会收到以下错误:

LDAPError - TYPE_OR_VALUE_EXISTS: {'info': u'00002083: AtrErr: DSID-031519F7, #5:\n\t0: 00002083: DSID-031519F7, problem 1006 (ATT_OR_VALUE_EXISTS), data 0, Att 150029 (mobile):len 2\n\t1: 00002083: DSID-031519F7, problem 1006 (ATT_OR_VALUE_EXISTS), data 0, Att 150029 (mobile):len 2\n\t2: 00002083: DSID-031519F7, problem 1006 (ATT_OR_VALUE_EXISTS), data 0, Att 150029 (mobile):len 2\n\t3: 00002083: DSID-031519F7, problem 1006 (ATT_OR_VALUE_EXISTS), data 0, Att 150029 (mobile):len 2\n\t4: 00002083: DSID-031519F7, problem 1006 (ATT_OR_VALUE_EXISTS), data 0, Att 150029 (mobile):len 2\n', 'desc': u'Type or value exists'}

使用命令行工具ldapmodify,我能够完成这两个操作:

dn:CN=common name,OU=Users,OU=x,DC=y,DC=z,DC=e
changetype: modify
add: mobile
mobile: +1 2345 6789

dn:CN=common name,OU=Users,OU=x,DC=y,DC=z,DC=e
changetype: modify
delete: mobile
mobile: +1 2345 6789

但无法做到这一点:

dn:CN=common name,OU=Users,OU=x,DC=y,DC=z,DC=e
changetype: modify
replace: mobile
mobile: +1 2345 6789
mobile: +4 567 89012345

以下错误:

ldap_modify: 约束违规 (19)           附加信息:00002081:AtrErr:DSID-03151907,#1:           0:00002081:DSID-03151907,问题1005(CONSTRAINT_ATT_TYPE),数据0,Att 150029(mobile)

我已经尝试了一段时间,真的很需要帮助。

1个回答

2
不用在意这个问题。已经替换:
nr1 = '+4712781271232'
nr2 = '+9812391282822'

old = {name:nr1}
new = {name:nr2}

With:

old = {'mobile':["+4712781271232"]}
new = {'mobile':["+9812391282822"]}

括号就是诀窍呢 ;)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接