1. 程式人生 > >域帳號密碼過期郵件提醒

域帳號密碼過期郵件提醒

python 密碼過期提醒 域控

思路,通過AD工具取得全部OU下人員工號、郵箱地址,再檢測工號多少天沒修改密碼,超過設定的天數,就執行發送郵件

# -*- coding:utf-8 -*-
import smtplib
import os
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

#jj_gonghao=['jjtest001','jjtest002','jjtest003']
dont_check = ['jjtest004','jjtest005']
days = '40'
def sendmail(gonghao,days):
    print('days = ' + days +'\n')
    my_sender='[email protected]'
    my_pass = 'p@ssw0rd'
    print('get email'+ gonghao)
    z = os.popen('dsquery user -samid '+ gonghao +' | dsget user -email').read()
#    print(z)
    email = z.split('\n') 
    print(email)
    my_user= email[1].strip()
    print(my_user)
    if '@airmate-jiu' not in my_user:
        print(gonghao + 'dont have email')
    else:   
        print('gonghao is :'+ my_user)
        msg=MIMEText('溫馨提醒:您的工號'+gonghao+'密碼已使用超過'+ days +'天,即將過期,為避免影響您的工作,建議及時修改。修改密碼方法:<br/>方法一:按CTRL+ALT+DEL 鍵後,在彈出界面選擇 更改密碼&nbsp;<br/>方法二:通過進入鏈接&nbsp;<a href="https://ip_addr/iisadmpwd/">https://ip/iisadmpwd/</a>&nbsp;進行修改(如提示此網站安全證書有問題,點擊繼續瀏覽此網站即可)','html','utf-8')
        msg['From']=formataddr(["郵件發件名",my_sender]) 
        msg['To']=formataddr([my_user,my_user]) 	
        msg['Subject']='溫馨提醒:您的工號'+gonghao+'開機密碼即將過期,請及時修改'           
        
        server=smtplib.SMTP("SMTPSERVER_ADDR")
        server.login(my_sender, my_pass)
        server.sendmail(my_sender,[my_user,],msg.as_string())  
        server.quit()
        print('send mail success...\n')

#print('===============================now is check password not change =========')
info=os.popen("dsquery user ou=XXX,dc=XXX,dc=XXX  -stalepwd " + str(days) + " -limit 0 -o upn").read()
info=info.split('\n')
while '' in info:info.remove('')
print(info)
for i in info:
#print(' i is =================================' + i + '\n')
    tmp = eval(i)
#    print(' eval is ================================' + tmp + '\n') 
    CN_NAME = tmp.split('@')
    gonghao = CN_NAME[0]
    try:
#        if gonghao in jj_gonghao:
            if gonghao not in dont_check:
                print('=========================\n' + 'now sendmail funcntion ...')
                sendmail(gonghao,days)
    except:
        print('error')



域帳號密碼過期郵件提醒