{"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":"\nLdap \u6279\u91cf\u6dfb\u52a0\u7528\u6237\u811a\u672c - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ldap \u6279\u91cf\u6dfb\u52a0\u7528\u6237\u811a\u672c - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\" \/>\n<meta property=\"og:description\" content=\"ldapadd.py #-*- coding:utf-8 -*- from ldap3 import Serv\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-07T02:21:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-29T09:36:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/image-1301591444.cos.ap-beijing.myqcloud.com\/20210407101827.png\" \/>\n<meta name=\"author\" content=\"\u7ba1\u7406\u5458\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/\"},\"author\":{\"name\":\"\u7ba1\u7406\u5458\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"headline\":\"Ldap \u6279\u91cf\u6dfb\u52a0\u7528\u6237\u811a\u672c\",\"datePublished\":\"2021-04-07T02:21:38+00:00\",\"dateModified\":\"2023-03-29T09:36:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/\"},\"wordCount\":4,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"articleSection\":[\"Linux\",\"Python\"],\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/\",\"url\":\"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/\",\"name\":\"Ldap \u6279\u91cf\u6dfb\u52a0\u7528\u6237\u811a\u672c - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\",\"isPartOf\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#website\"},\"datePublished\":\"2021-04-07T02:21:38+00:00\",\"dateModified\":\"2023-03-29T09:36:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/#breadcrumb\"},\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.linuxdevops.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\",\"item\":\"https:\/\/www.linuxdevops.cn\/linux\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Ldap \u6279\u91cf\u6dfb\u52a0\u7528\u6237\u811a\u672c\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#website\",\"url\":\"https:\/\/www.linuxdevops.cn\/\",\"name\":\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\",\"description\":\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\u7b14\u8bb0\",\"publisher\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"alternateName\":\"linuxdevops\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.linuxdevops.cn\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-CN\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\",\"name\":\"\u7ba1\u7406\u5458\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-CN\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg\",\"contentUrl\":\"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg\",\"width\":512,\"height\":512,\"caption\":\"\u7ba1\u7406\u5458\"},\"logo\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/\"},\"description\":\"\u7ba1\u7406\u5458\",\"url\":\"https:\/\/www.linuxdevops.cn\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Ldap \u6279\u91cf\u6dfb\u52a0\u7528\u6237\u811a\u672c - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/","og_locale":"zh_CN","og_type":"article","og_title":"Ldap \u6279\u91cf\u6dfb\u52a0\u7528\u6237\u811a\u672c - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","og_description":"ldapadd.py #-*- coding:utf-8 -*- from ldap3 import Serv","og_url":"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/","og_site_name":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","article_published_time":"2021-04-07T02:21:38+00:00","article_modified_time":"2023-03-29T09:36:12+00:00","og_image":[{"url":"https:\/\/image-1301591444.cos.ap-beijing.myqcloud.com\/20210407101827.png"}],"author":"\u7ba1\u7406\u5458","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/#article","isPartOf":{"@id":"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/"},"author":{"name":"\u7ba1\u7406\u5458","@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"headline":"Ldap \u6279\u91cf\u6dfb\u52a0\u7528\u6237\u811a\u672c","datePublished":"2021-04-07T02:21:38+00:00","dateModified":"2023-03-29T09:36:12+00:00","mainEntityOfPage":{"@id":"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/"},"wordCount":4,"commentCount":0,"publisher":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"articleSection":["Linux","Python"],"inLanguage":"zh-CN","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/","url":"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/","name":"Ldap \u6279\u91cf\u6dfb\u52a0\u7528\u6237\u811a\u672c - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","isPartOf":{"@id":"https:\/\/www.linuxdevops.cn\/#website"},"datePublished":"2021-04-07T02:21:38+00:00","dateModified":"2023-03-29T09:36:12+00:00","breadcrumb":{"@id":"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/#breadcrumb"},"inLanguage":"zh-CN","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.linuxdevops.cn\/2021\/04\/batch-adding-user-scripts-in-ldap\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.linuxdevops.cn\/"},{"@type":"ListItem","position":2,"name":"Linux","item":"https:\/\/www.linuxdevops.cn\/linux\/"},{"@type":"ListItem","position":3,"name":"Ldap \u6279\u91cf\u6dfb\u52a0\u7528\u6237\u811a\u672c"}]},{"@type":"WebSite","@id":"https:\/\/www.linuxdevops.cn\/#website","url":"https:\/\/www.linuxdevops.cn\/","name":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","description":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\u7b14\u8bb0","publisher":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"alternateName":"linuxdevops","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.linuxdevops.cn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-CN"},{"@type":["Person","Organization"],"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1","name":"\u7ba1\u7406\u5458","image":{"@type":"ImageObject","inLanguage":"zh-CN","@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/","url":"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg","contentUrl":"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg","width":512,"height":512,"caption":"\u7ba1\u7406\u5458"},"logo":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/"},"description":"\u7ba1\u7406\u5458","url":"https:\/\/www.linuxdevops.cn\/author\/root\/"}]}},"_links":{"self":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/1012"}],"collection":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/comments?post=1012"}],"version-history":[{"count":4,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/1012\/revisions"}],"predecessor-version":[{"id":1213,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/1012\/revisions\/1213"}],"wp:attachment":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/media?parent=1012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/categories?post=1012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/tags?post=1012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}