Python LDAP 将属性写入 Active Directory
Python LDAP write attribute to Active Directory
我可以通过 python-ldap 绑定和查询 Active Directory,除了在 AD 上添加或修改属性时没有任何问题。我可以添加该属性,但由于所有文本都是乱码,因此编码似乎有偏差。
我试过用 utf8 和其他一些方法对我的字符串进行编码,但没有成功。
我还尝试绑定域管理员帐户以及绑定我将更改属性的用户帐户,结果相同。
这是我用来更新属性的方法:
class LdapHelpers:
def __init__(self):
import ldap
# set globals
self.server = 'LDAP://dc.mycompany.com'
self.admin_dn = 'CN=Administrator,CN=users,DC=mycompany,DC=com'
self.admin_pass = 'coolpassword'
# init LDAP connection
#ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, 0)
ldap.set_option(ldap.OPT_REFERRALS, 0)
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
ldap.protocol_version = ldap.VERSION3
self.ldap = ldap.initialize(self.server)
def update_attribute(self, attrib, value):
try:
import ldap
conn = self.ldap
conn.simple_bind_s(self.admin_dn, self.admin_pass)
mod_attrs = [( ldap.MOD_REPLACE, "mobile", "6306564123")]
# I have tried other variations of the above
# mod_attrs = [( ldap.MOD_REPLACE, "mobile", "6306564123".encode('utf-8)]
conn.modify_s('CN=Mike Smith,OU=GoogleApps,DC=company,DC=com', mod_attrs)
print 'record updated'
except ldap.LDAPError as e:
return e.message
通过终端执行 ldapsearch 这就是属性的样子:
mobile:: MC8sAQAAAAAQNA==
这是 'Hello World' 设置为移动设备后的样子:
mobile:: 77+9ehsCAAAAABDvv70V
我查过 MSDN,它说 ldap 属性只是一个 Unicode 字符串。
系统:Ubuntu 15.10 64 位
Python:2.7.10
python-ldap==2.4.21
附带说明一下,我可以毫无问题地搜索 AD 并且 parse/display 返回了用户属性,问题似乎只与创建或修改此编码问题所涉及的属性有关。
末尾的“=”通常表示它是 Base64 encoding. Python has a standard library for encoding/decoding base64 (The link is for Python 3, but Python 2 also has the library). LDAP does indeed use Base64 for something. See The LDAP Data Interchange Format (LDIF)。
查看 pyad 中的代码以阐明要做什么:https://pypi.python.org/pypi/pyad
基于Python。
已回答问题的另一个示例:
好的,我发现发生了什么,我使用 PyPy 4.0.1 作为解释器,出于某种原因,这导致 python-ldap 库 and/or 编码出现问题字符串。
我为解释器切换回 Python 2.7.10,现在使用 python-ldap 库,上面相同的修改命令按预期工作。因此,如果使用 PyPy 和这个特定的库,一定要小心……
我可以通过 python-ldap 绑定和查询 Active Directory,除了在 AD 上添加或修改属性时没有任何问题。我可以添加该属性,但由于所有文本都是乱码,因此编码似乎有偏差。
我试过用 utf8 和其他一些方法对我的字符串进行编码,但没有成功。
我还尝试绑定域管理员帐户以及绑定我将更改属性的用户帐户,结果相同。
这是我用来更新属性的方法:
class LdapHelpers:
def __init__(self):
import ldap
# set globals
self.server = 'LDAP://dc.mycompany.com'
self.admin_dn = 'CN=Administrator,CN=users,DC=mycompany,DC=com'
self.admin_pass = 'coolpassword'
# init LDAP connection
#ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, 0)
ldap.set_option(ldap.OPT_REFERRALS, 0)
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
ldap.protocol_version = ldap.VERSION3
self.ldap = ldap.initialize(self.server)
def update_attribute(self, attrib, value):
try:
import ldap
conn = self.ldap
conn.simple_bind_s(self.admin_dn, self.admin_pass)
mod_attrs = [( ldap.MOD_REPLACE, "mobile", "6306564123")]
# I have tried other variations of the above
# mod_attrs = [( ldap.MOD_REPLACE, "mobile", "6306564123".encode('utf-8)]
conn.modify_s('CN=Mike Smith,OU=GoogleApps,DC=company,DC=com', mod_attrs)
print 'record updated'
except ldap.LDAPError as e:
return e.message
通过终端执行 ldapsearch 这就是属性的样子:
mobile:: MC8sAQAAAAAQNA==
这是 'Hello World' 设置为移动设备后的样子:
mobile:: 77+9ehsCAAAAABDvv70V
我查过 MSDN,它说 ldap 属性只是一个 Unicode 字符串。
系统:Ubuntu 15.10 64 位 Python:2.7.10 python-ldap==2.4.21
附带说明一下,我可以毫无问题地搜索 AD 并且 parse/display 返回了用户属性,问题似乎只与创建或修改此编码问题所涉及的属性有关。
末尾的“=”通常表示它是 Base64 encoding. Python has a standard library for encoding/decoding base64 (The link is for Python 3, but Python 2 also has the library). LDAP does indeed use Base64 for something. See The LDAP Data Interchange Format (LDIF)。
查看 pyad 中的代码以阐明要做什么:https://pypi.python.org/pypi/pyad
基于Python。
已回答问题的另一个示例:
好的,我发现发生了什么,我使用 PyPy 4.0.1 作为解释器,出于某种原因,这导致 python-ldap 库 and/or 编码出现问题字符串。
我为解释器切换回 Python 2.7.10,现在使用 python-ldap 库,上面相同的修改命令按预期工作。因此,如果使用 PyPy 和这个特定的库,一定要小心……