{"id":1012,"date":"2021-04-07T10:21:38","date_gmt":"2021-04-07T02:21:38","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=1012"},"modified":"2023-03-29T17:36:12","modified_gmt":"2023-03-29T09:36:12","slug":"batch-adding-user-scripts-in-ldap","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/","title":{"rendered":"Ldap \u6279\u91cf\u6dfb\u52a0\u7528\u6237\u811a\u672c"},"content":{"rendered":"
ldapadd.py<\/p>\n
#-*- coding:utf-8 -*-\n\nfrom ldap3 import Server, Connection, ALL\n\n# define the server\ns = Server(host='ldap.example.com', port=389,get_info=ALL)\n\n# define the connection\nc = Connection(s, user='cn=admin,dc=example,dc=com', password='123456')\n\n# perform the Bind operation\nif not c.bind():\n print('error in bind', c.result)\n#ldap\u6dfb\u52a0\u7528\u6237\ndef adduser(line,ldap_dn,ldap_attributes):\n c.add(dn=ldap_dn,\n object_class=['inetOrgPerson', 'top'],\n attributes=ldap_attributes)\n #\u6253\u5370\u62a5\u9519\u4fe1\u606f\u548c\u62a5\u9519\u6570\u636e\n if c.result['result'] != 0:\n print(c.result,' ===\u6dfb\u52a0\u5931\u8d25===> ' + line)\n\nwith open('E:\\python\\ldapuser','r',encoding='utf8') as f:\n for line in f.readlines():\n ldap_attributes = {}\n dn_cn=(line.replace('\\n','').split('|')[0])\n dn_sn=(line.replace('\\n','').split('|')[1])\n # dn_ou=(line.replace('\\n','').split('|')[2])\n ldap_password=(line.replace('\\n','').split('|')[2])\n ldap_mail=(line.replace('\\n','').split('|')[3])\n ldap_mobile=(line.replace('\\n','').split('|')[4])\n ldap_dn='cn=%s,ou=\u6280\u672f\u90e8,dc=example,dc=com'%(dn_cn)\n ldap_attributes={'sn': dn_sn,\n 'userpassword': ldap_password,\n 'mail': ldap_mail,\n 'mobile': ldap_mobile}\n # print(ldap_dn,ldap_attributes)\n adduser(line,ldap_dn,ldap_attributes)\n\nc.unbind()<\/code><\/pre>\n<\/p>\n
ldapuser<\/p>\n
wangfeng|\u738b\u4e94|123456|wangwu@example.com|15012345678\nzhangsan|\u5f20\u4e09|123456|zhangsan@example.com|15012345678<\/code><\/pre>\n#\u8bbe\u7f6e\u5bc6\u7801\u7684\u65f6\u5019\u6700\u597d\u4e0d\u8981\u6709 | \ncn | sn | password | mail | mobile<\/code><\/pre>\n<\/p>\n","protected":false},"excerpt":{"rendered":"
ldapadd.py #-*- coding:utf-8 -*- from ldap3 import Serv<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,13],"tags":[],"yoast_head":"\n
Ldap \u6279\u91cf\u6dfb\u52a0\u7528\u6237\u811a\u672c - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n