目前运维开发平台还在搭建中,这个脚本是给各位开发人员提供一份可读账号,脚本里我把账号和密码已经写死了,如果需要拓展可以自行设定变量。


整个脚本内容如下:

#!/usr/bin/env python
#coding=utf-8
#Writed by Chris Chan@2017-03-07

import os
import time
import commands
import pexpect

def AddUser() :
    ret = ''
    ret = commands.getoutput("grep '账号名' /etc/passwd")
    if '' == ret :
        try :
            print ("用户不存在,开始新建用户\n%s" %ret)
            commands.getoutput("useradd xuchunbo")
        except:
            (ErrorType, ErrorValue, ErrorTB) = sys.exc_info()                               #Connect异常,收集错误信息,不退出脚本
            print ("新建用户失败,错误信息: \n%s" %ErrorValue)
    else :
        print ("用户已存在,无需新建用户\n%s" %ret)
    try :
        child = pexpect.spawn("passwd 账号名", timeout = 5)
        child.expect("New password:")
        child.sendline("密码")
        child.expect("Retype new password:")
        child.sendline("密码")
        child.expect("passwd: all authentication tokens updated successfully")
        
    except pexpect.EOF :
        pass
    except pexpect.TIMEOUT :
        pass
    except :
        (ErrorType, ErrorValue, ErrorTB) = sys.exc_info()                                   #Connect异常,收集错误信息,不退出脚本
        print ("修改用户密码失败,错误信息: \n%s" %ErrorValue)                              #打印错误信息
    finally :
        print ("用户密码修改成功!\n")
        
if __name__ == '__main__' :
    AddUser()


如果批处理的话,就搭配ansible操作即可。