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)
原文地址:https://www.cnblogs.com/oldghost/p/8478967.html