首页 > 编程语言 >Python3禁用AD账号与重置AD账号密码

Python3禁用AD账号与重置AD账号密码

时间:2024-07-16 16:29:37浏览次数:8  
标签:dn AD dc Python3 conn print attributes ldap3 账号密码

Python3禁用AD账号

# LDAP服务器地址、端口号及连接参数
import ldap3
from ldap3 import Server, Connection,ALL 
server = Server('10.10.22.14',port=389,get_info=ALL)
conn = Connection(server, user='admin', password='yyy',auto_bind=True)

username='zdx'

if conn.bind():

   # 设置查询条件
   #base_dn = 'OU=IT,OU=SOU,dc=test19,dc=com'
   base_dn = 'dc=test19,dc=com'
   #filterstr = "(objectClass=person)"
   filterstr=f'(&(objectClass=Person)(sAMAccountName={username}))'
   #print(filterstr)
   # 发起查询并获取结果
   result = conn.search(base_dn, filterstr, attributes=['cn', 'mail', 'distinguishedName', 'sAMAccountName'])
   #print(result,conn.response)
   if result:
       res = conn.response
       entry = res[0]
       if 'raw_attributes' in entry.keys():
          samaccountname = str(entry['raw_attributes']['sAMAccountName'][0].lower(),'utf-8')
          dn = str(entry['raw_attributes']['distinguishedName'][0].lower(),'utf-8')
          print(dn,samaccountname)

           # 禁用账户
          mod_attrs = {
              'userAccountControl': [(ldap3.MODIFY_REPLACE, [514])]  # 514 表示禁用账户
          }
          conn.modify(dn, mod_attrs)
           
          # 检查修改是否成功
          if conn.result["description"] == "success":
              print(f"Account {dn} has been disabled.")
          else:
              print(f"Failed to disable account {dn}.")

       else:
           print(f"{username} not found.")
   else:
       print("No entries found.")

else:
    print("Failed to bind with the server.")

# 关闭连接
conn.unbind()

 

Python3使用管理员权限重置AD账号密码

# LDAP服务器地址、端口号及连接参数
import ldap3
from ldap3 import Server, Connection,ALL 
server = Server('10.10.22.14',port=636,get_info=ALL,use_ssl=True)  #修改密码必须要使用ldaps
conn = Connection(server, user='admin', password='Yyy',auto_bind=True)

username='zdx'
new_pwd='Y11'

if conn.bind():

   # 设置查询条件
   #base_dn = 'OU=IT,OU=SOU,dc=test19,dc=com'
   base_dn = 'dc=test19,dc=com'
   #filterstr = "(objectClass=person)"
   filterstr=f'(&(objectClass=Person)(sAMAccountName={username}))'
   #print(filterstr)
   # 发起查询并获取结果
   result = conn.search(base_dn, filterstr, attributes=['cn', 'mail', 'distinguishedName', 'sAMAccountName'])
   #print(result,conn.response)
   if result:
       res = conn.response
       entry = res[0]
       if 'raw_attributes' in entry.keys():
          samaccountname = str(entry['raw_attributes']['sAMAccountName'][0].lower(),'utf-8')
          dn = str(entry['raw_attributes']['distinguishedName'][0].lower(),'utf-8')
          print(dn,samaccountname)

          pwd_change={
            'userPassword':[(ldap3.MODIFY_REPLACE,[new_pwd])],
            'unicodePwd':[(ldap3.MODIFY_REPLACE,[f'"{new_pwd}"'.encode('utf-16-le')])],
            'userAccountControl':[(ldap3.MODIFY_REPLACE,[512])]
          }

          conn.modify(dn,pwd_change)
           

       else:
           print(f"{username} not found.")
   else:
       print("No entries found.")

else:
    print("Failed to bind with the server.")

# 关闭连接
conn.unbind()

 

参考:https://blog.51cto.com/u_13366251/7418664

https://www.cnblogs.com/cptao/p/15515047.html 使用旧密码修改新密码

 

标签:dn,AD,dc,Python3,conn,print,attributes,ldap3,账号密码
From: https://www.cnblogs.com/dreamer-fish/p/18305554

相关文章

  • 图扑低代码数字孪生 Web SCADA 智慧钢厂
    2024年4月,中国钢铁工业协会发布了《钢铁行业数字化转型评估报告(2023年)》(以下简称《报告》)。《报告》指出,绝大部分钢铁企业建立了数字化转型相关管理组织和团队,并加强其规划落实,系统间的综合集成能力进一步加强。在研发、制造、服务全生命周期管控以及产业链协同等方面需继续深......
  • [2024] springboot Hadoop技术下的校园二手交易系统的设计与实现
    博主介绍:✌CSDN新星计划导师、Java领域优质创作者、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌技术范围:SpringBoot、Vue、SSM、HTML、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app、大数......
  • C. Load Balancing
    原题链接题解找出大于对应平均数的部分。。。code#include<bits/stdc++.h>#definelllonglongusingnamespacestd;constllN=114514;lla[100006];voidsolve(){lln;cin>>n;llsum=0;for(inti=1;i<=n;i++){cin>>a[i];......
  • TaD+RAG-缓解大模型“幻觉”的组合新疗法
    TaD:任务感知解码技术(Task-awareDecoding,简称TaD),京东联合清华大学针对大语言模型幻觉问题提出的一项技术,成果收录于IJCAI2024。RAG:检索增强生成技术(Retrieval-augmentedGeneration,简称RAG),是业内解决LLM幻觉问题最有效的系统性方案。1.背景介绍近来,以ChatGPT为代表的生成式大......
  • 随手记:Bruno动态注入Header
    因为PostMan启动太慢,动不动就要登录,以及防火墙的问题,搞起来挺麻烦,一气之下就换了Bruno来管理API请求,接口的安全校验也是很正常的事儿,最近有个兄弟部门使用了参数+时间戳+HmacSHA256校验,把校验的Sign放到Header里,研究了下,做个记录,方便随取随用,这种动态的Header需要使用Script:cons......
  • phpMyAdmin
    phpMyAdminhttps://www.phpmyadmin.net/phpMyAdminisafreesoftwaretoolwritteninPHP,intendedtohandletheadministrationofMySQLovertheWeb.phpMyAdminsupportsawiderangeofoperationsonMySQLandMariaDB.Frequentlyusedoperations(managi......
  • pthread_cond_wait
    介绍pthread_cond_wait是一个阻塞函数,它会将当前线程放入条件变量的等待队列中,并自动释放与之关联的互斥锁。当条件变量被其他线程用pthread_cond_signal或pthread_cond_broadcast唤醒后,pthread_cond_wait会自动重新获取互斥锁,然后再返回。这个机制可以避免死锁,并确保......
  • 机器学习策略篇:详解处理数据不匹配问题(Addressing data mismatch)
    处理数据不匹配问题如果您的训练集来自和开发测试集不同的分布,如果错误分析显示有一个数据不匹配的问题该怎么办?这个问题没有完全系统的解决方案,但可以看看一些可以尝试的事情。如果发现有严重的数据不匹配问题,通常会亲自做错误分析,尝试了解训练集和开发测试集的具体差异。技术上......
  • elementui的el-cascader-panel在jsx里如何自定义label和props属性
    render(){return(<el-cascader-panelonChange={(val)=>{this.handleFormatChange(val,'format','dataColumns',indexInMap)}}props={{renderLabel:(params)=>{......
  • Python安装出现严重错误的解决方法_0x80070643-( A newer version of the Python laun
    每次在装软件配置环境的时候,总会遇到别人碰不到的各种问题,人都麻了。最后我还是自己尝试这解决了,只是建议,虽然说不知道是否以后还会问题,但是可以成功安装,配置环境并运行。(本人是win11)首先解释一下pythonlauncher是什么资料解释:PythonLauncher是Python官方提供的一个工具,......