gitlab☞批量修改用户密码

阅读量: zyh 2018-09-07 19:20:23
Categories: > Tags:
export OldToken=
export OldDomain=

# 导出 用户ID 用户名 用户状态 用户邮箱
curl -s --header "PRIVATE-TOKEN: ${OldToken}" "http://${OldDomain}/api/v3/users?per_page=100&page=1"  | jq '.[] | .id,.username,.state,.email' | sed 'N;N;N;s#\n# #g;s#"##g' | grep 'active' >> user.id;
curl -s --header "PRIVATE-TOKEN: ${OldToken}" "http://${OldDomain}/api/v3/users?per_page=100&page=2"  | jq '.[] | .id,.username,.state,.email' | sed 'N;N;N;s#\n# #g;s#"##g' | grep 'active' >> user.id;

# 修改用户密码, 并生成密码记录文件 user.password
while read userid username state email;do
    Pwd=`cat /proc/sys/kernel/random/uuid | cut -d'-' -f1`
    curl --request  PUT --header "PRIVATE-TOKEN:${OldToken}" --data "password=${username}@${Pwd}" "http://${OldDomain}/api/v3/users/${userid}"
    echo "${email} ${username} ${username}@${Pwd}" >> user.password
done < user.id
#!/usr/bin/python 
#coding:utf-8 
 
 
import smtplib 
from email.mime.text import MIMEText 
import sys 
 
mail_host = 'smtp.gmail.com:587'
mail_user = '[email protected]'
mail_pass = 'hoqmefjrmkvkukth'

def send_mail(to_list,subject,content): 
    me = mail_user+"<"+mail_user+">" 
    msg = MIMEText(content) 
    msg['Subject'] = subject 
    msg['From'] = me 
    msg['to'] = to_list 
     
    try: 
	    print "start sendmail"
        s = smtplib.SMTP(mail_host)
	    print "connect mail server suesscc" 
	    s.starttls()
        s.login(mail_user,mail_pass) 
	    print "login mail server suesscc"
        s.sendmail(me,to_list,msg.as_string()) 
        s.close() 
        return True 
    except Exception,e: 
        print str(e) 
        return False 
     
if __name__ == "__main__": 
    send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
while read email username pwd;do python sendmail.py "${email}" "gitlab ${username}新密码" "${pwd}";done < user.password