【问题标题】:How can I change password for domain user(windows Active Directory) using Python?如何使用 Python 更改域用户(Windows Active Directory)的密码?
【发布时间】:2014-01-02 03:06:18
【问题描述】:

如何使用 Python 更改域用户的密码?我有 ldap 模块,但没有解决方案。我设法通过ldap查询了当前的设置,但是如何修改呢?

import ldap
import sys

host = 'ldap://10.172.0.79'

con = ldap.initialize(host)
BIND_DN = "administrator@biztalk.com"
BIND_PASS = "a-123456"
con.set_option( ldap.OPT_X_TLS_DEMAND, True )
con.set_option( ldap.OPT_DEBUG_LEVEL, 255 )

PASSWORD_ATTR = "unicodePwd"
username="bizadmin"
user_dn = "CN=%s,OU=User,OU=biztalk,DC=biz-talk,DC=com" % username
password = 'New12345'

# Set AD password
unicode_pass = unicode("\"" + password + "\"", "iso-8859-1")
password_value = unicode_pass.encode("utf-16-le")
add_pass = [(ldap.MOD_REPLACE, PASSWORD_ATTR, [password_value])]

# Replace password
try:
    con.modify_s(user_dn, add_pass)
    print "Active Directory password for", username, "was set successfully!"
except ldap.LDAPError, e:
    sys.stderr.write('Error setting AD password for: ' + username + '\n')
    sys.stderr.write('Message: ' + str(e) + '\n')
    sys.exit(1)

错误

pydev 调试器:启动

设置 AD 密码时出错:bizadmin

消息:{'desc':“无法联系 LDAP 服务器”}


Python 更改域(Microsoft Active Directory)用户的密码。

...需要 python 和域之间的认证服务?

你有什么好的方法来处理它吗?

谢谢!

【问题讨论】:

    标签: python active-directory ldap


    【解决方案1】:

    此代码适用于 Windows 2012 R2 AD:

    首先安装最新的 ldap3 包: sudo pip install ldap

    #!/usr/bin/python
    
    import ldap3
    
    SERVER='127.0.0.1'
    BASEDN="DC=domain,DC=com"
    USER="user_domain_login_name@domain.com"
    CURREENTPWD="current_password"
    NEWPWD="new_password"
    
    SEARCHFILTER='(&(userPrincipalName='+USER+')(objectClass=person))'
    
    USER_DN=""
    USER_CN=""
    
    ldap_server = ldap3.Server(SERVER, get_info=ldap3.ALL)
    conn = ldap3.Connection(ldap_server, USER, CURREENTPWD, auto_bind=True)
    conn.start_tls()
    #print conn
    conn.search(search_base = BASEDN,
             search_filter = SEARCHFILTER,
             search_scope = ldap3.SUBTREE,
             attributes = ['cn', 'givenName', 'userPrincipalName'],
             paged_size = 5)
    
    for entry in conn.response:
        if entry.get("dn") and entry.get("attributes"):
            if entry.get("attributes").get("userPrincipalName"):
                if entry.get("attributes").get("userPrincipalName") == USER:
                    USER_DN=entry.get("dn")
                    USER_CN=entry.get("attributes").get("cn")
    
    print "Found user:", USER_CN
    print USER_DN
    print ldap3.extend.microsoft.modifyPassword.ad_modify_password(conn, USER_DN, NEWPWD, CURREENTPWD,  controls=None)
    

    【讨论】:

      【解决方案2】:

      Python 不是我的语言,但我会通过 LDAP 更改 Active-Directory 密码。

      就您的网址而言:

      您的 LDAP URL 应该是这样的:

      host = 'LDAP://10.172.0.79/dc=directory,dc=example,dc=com'
      

      使用 'LDAP' 而不是 'ldap' 和后面的好目录路径。

      就密码而言:

      首先:据我了解,只有当您的服务器有证书并且您通过 LDAPS (SSL) 联系时,您才能更改 AD 密码 unicode_pass

      第二:密码用双引号给出,密码test.2006变成“test.2006”。

      第三:resutl必须用unicode编码。


      已编辑:

      安装证书服务器后,您只需重新启动服务器即可让 AD 在端口 636 (LDAPS) 上等待。在 Python 方面,这是我发现的:

      ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
      l = ldap.initialize("LDAPS://10.172.0.79:636")
      l.set_option(ldap.OPT_REFERRALS, 0)
      l.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
      l.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND)
      l.set_option( ldap.OPT_X_TLS_DEMAND, True )
      l.set_option( ldap.OPT_DEBUG_LEVEL, 255 )
      l.simple_bind_s("admin@tester.com","password")
      

      【讨论】:

      • 如何设置我的服务器有证书并通过 LDAPS(SSL) 联系?我在我的服务器中安装了证书服务器,但我无法与 ldap 联系。如果你有一步一步,你能分享给我吗?谢谢!
      • 感谢您的帮助。但它不起作用。在 Python 端,不需要证书文件......?
      • 在 SSL 中,您需要导入证书颁发机构的公钥证书。
      【解决方案3】:

      密码更改代码看起来很完美。

      初始化后你没有绑定。绑定是必须的。

      con.simple_bind_s(user, pass)
      

      此外,对于初学者,您可以通过设置此选项来忽略绑定的证书错误。一旦您能够更新密码,您可以根据需要强化证书。

      con.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-17
        • 2012-06-26
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多