zoukankan      html  css  js  c++  java
  • python create home dircetory

    #!/bin/env python
    # coding:utf-8
    import ldap
    import ldif
    import sys
    import os
    # ad服务器ip
    conn = ldap.initialize('ldap://10.10.1.1')
    conn.protocol_version = 3
    conn.set_option(ldap.OPT_REFERRALS, 0)
    # 登录
    conn.simple_bind_s('liunx_ad@ghost.com', 'linux_ad')
    ldif_writer = ldif.LDIFWriter(sys.stdout)
    # 筛选条件  OU是你文件夹的名称 DC是域名的拆分 cn=* 查询所有用户
    basedn = "OU=GHOST,DC=ghost,DC=com"
    scope = ldap.SCOPE_SUBTREE
    filter = '(&(objectClass=user)(!(objectClass=computer))(unixHomeDirectory=*))'
    attr = ['uid', 'useraccountcontrol', 'uidNumber', 'unixHomeDirectory']
    results = conn.search_s(basedn, scope, filter, attr)
    def create_user_home(path,user,uid):
        full_path = os.path.join(path,user)
        disable_path = full_path + '.disable'
        if os.path.isdir(disable_path):
           os.rename(disable_path, full_path)
        if not os.path.isdir(full_path):
           os.makedirs(full_path, 0700)
           os.chown(full_path, uid, 10000)
           bashrc_file = os.path.join(full_path, '.bashrc')
           file = open(bashrc_file, 'w')
           file.write(bashrc)
           os.chown(bashrc_file, uid, 10000)
    bashrc = r"""if [ -f /etc/bashrc ];then
            . /etc/bashrc
                    fi   
                    """
        
    for dn,entry in results:
        user = entry['uid'][0]
        uid = int(entry['uidNumber'][0])
        create_user_home("/amount",user,uid)
        #ldif_writer.unparse(dn,entry)
  • 相关阅读:
    移动端 异常捕获
    禁止选中网页的某段文字
    Java正则表达式的解释说明
    error while performing database login with the xxx driver
    javascript 日期转换为中文
    chrono使用
    resize
    github使用
    adb 无法连接 CreateProcess failure, error 2 * could not start server *
    opencv-videowriter
  • 原文地址:https://www.cnblogs.com/oldghost/p/8478967.html
Copyright © 2011-2022 走看看