首页 > 解决方案 > python-ldap 修改电话号码

问题描述

我想用 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'}

我可以通过将 old 留空来删除该条目,但是当我尝试设置它时,我得到以下信息:

LDAPError - TYPE_OR_VALUE_EXISTS: {'info': u'00002083: AtrErr: DSID-031519F7, #5:\n\t0: 00002083: DSID-031519F7, 问题 1006 (ATT_OR_VALUE_EXISTS), 数据 0, Att 150029 (mobile):len 2 \n\t1: 00002083: DSID-031519F7, 问题 1006 (ATT_OR_VALUE_EXISTS), 数据 0, Att 150029 (mobile):len 2\n\t2: 00002083: DSID-031519F7, 问题1006 (ATT_OR_VALUE_EXISTS), 数据0, Att 15002 (移动):len 2\n\t3:00002083:DSID-031519F7,问题1006(ATT_OR_VALUE_EXISTS),数据0,Att 150029(移动):len 2\n\t4:00002083:DSID-031519F7,问题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)

现在已经尝试了一段时间,非常感谢一些帮助。

标签: pythonldappython-ldap

解决方案


别管这个问题。替换:

nr1 = '+4712781271232'
nr2 = '+9812391282822'

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

和:

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

括号可以解决问题;)


推荐阅读