#!/usr/bin/env python # coding=UTF-8 '''
https://www.cnblogs.com/haiya2019/p/10627730.html
@Author: wjx @Description: AD域 @Date: 2018-12-23 21:23:57 @LastEditTime: 2019-03-28 23:46:56 ''' from ldap3 import Server, Connection, ALL, NTLM class Adoper(): ''' 操作AD域的类 ''' def __init__(self, domain, ip, admin='administrator', pwd=None): ''' domain: 域名,格式为:xxx.xxx.xxx ip: ip地址,格式为:192.168.214.1 admin: 管理员账号 pwd: 管理员密码 ''' self.domain = domain self.DC = ','.join(['DC=' + dc for dc in domain.split('.')]) # csc.com -> DC=csc,DC=com self.pre = domain.split('.')[0].upper() # 用户登陆的前缀 self.ip = ip self.admin = admin self.pwd = pwd self.server = Server(self.ip, get_info=ALL) self.conn = Connection(self.server, user=self.pre+'\\'+self.admin, password=self.pwd, auto_bind=True, authentication=NTLM) def search(self, org): ''' 查询组织下的用户 org: 组织,格式为:aaa.bbb 即bbb组织下的aaa组织,不包含域地址 ''' att_list = ['displayName', 'userPrincipalName','userAccountControl','sAMAccountName','pwdLastSet'] org_base = ','.join(['OU=' + ou for ou in org.split('.')]) + ',' + self.DC res = self.conn.search(search_base=org_base, search_filter='(objectclass=user)', # 查询数据的类型 attributes=att_list, # 查询数据的哪些属性 paged_size=1000) # 一次查询多少数据 if res: for user in self.conn.entries: yield user['displayName'] else: print('查询失败: ', self.conn.result['description']) return None def add_org(self, org): ''' 增加组织 oorg: 组织,格式为:aaa.bbb 即bbb组织下的aaa组织,不包含域地址 ''' org_base = ','.join(['OU=' + ou for ou in org.split('.')]) + ',' + self.DC res = self.conn.add(org_base, object_class='OrganizationalUnit') # 成功返回True,失败返回False if res: print(f'增加组织[ {org} ]成功!') else: print(f'增加组织[ {org} ]发生错误: ', self.conn.result['description']) def add_user(self, org, name, uid): ''' 增加用户 org:增加到该组织下 name:显示名称 uid:账号 ''' org_base = ','.join(['OU=' + ou for ou in org.split('.')]) + ',' + self.DC user_att = { 'displayName' : name, 'userPrincipalName' : uid + '@' + self.domain, # uid@admin组成登录名 'userAccountControl': '544', # 启用账号 'sAMAccountName': uid, 'pwdLastSet': -1 # 取消下次登录需要修改密码 } res = self.conn.add(f'CN={uid},{org_base}', object_class='user', attributes=user_att) if res: print(f'增加用户[ {name} ]成功!') else: print(f'增加用户[ {name} ]发生错误:', self.conn.result['description']) if __name__ == '__main__': ad93 = Adoper(domain='test.csc.com', ip='192.168.214.93', pwd='Winhong1234@#test') for user in ad93.search('信息科技部.总行.cibuser'): print(user) ad93.add_org('python02.cibuser') ad93.add_user('python02.cibuser', 'python03类用户', 'python03')
标签:domain,AD,self,base,user,conn,org,操作,Python3 From: https://www.cnblogs.com/tanaikang/p/18390674