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)
  • 相关阅读:
    plsql developer中各个window的作用【转】
    回忆java输入输出流,走出误区
    JDBC中的元数据
    对于Oracle、mysql和sql server中的部分不同理解
    我对数据库事务的理解(MYSQL中)
    关于mysql的备份和恢复
    mysql触发器学习
    mysql存储过程学习
    JavaScript位运算符
    【JavaScript】数组随机排序 之 Fisher–Yates 洗牌算法
  • 原文地址:https://www.cnblogs.com/oldghost/p/8478967.html
Copyright © 2011-2022 走看看