#!/usr/bin/env python
import ldap3
from ldap3 import Server, Connection, ALL, MODIFY_REPLACE, SUBTREE
import xlwings as xw
import datetime
import psutil
import subprocess
def is_excel_running():
for proc in psutil.process_iter(['name']):
if proc.info['name'] == 'EXCEL.EXE':
return True
return False
def kill_excel():
for proc in psutil.process_iter(['name', 'pid']):
if proc.info['name'] == 'EXCEL.EXE':
proc.kill()
def open_excel():
AB_dict = {}
data_A = sheet.range('A2').expand('down').value
data_B = sheet.range('B2').expand('down').value
data_A_list = [str(item).split('.')[0] if isinstance(item, float) else str(item) for item in data_A]
data_B_list = [str(item).split('.')[0] if isinstance(item, float) else str(item) for item in data_B]
for a, b in zip(data_A_list, data_B_list):
AB_dict.update({a: b})
data = list(AB_dict.keys())
data2 = list(AB_dict.values())
for i in data:
own_id.append(str(i))
for ii in data2:
leader_id.append(str(ii))
def find_dn():
for own in own_id:
try:
c.search(dn, f"(samAccountName={own})", SUBTREE, attributes=['distinguishedName'])
own_dn = c.response[0]['attributes']['distinguishedName']
own_dn_lst.append(own_dn)
except:
own_dn_lst.append('查询不到')
for leader in leader_id:
try:
c.search(dn, f"(samAccountName={leader})", SUBTREE, attributes=['distinguishedName'])
own_dn = c.response[0]['attributes']['distinguishedName']
lead_dn_lst.append(own_dn)
except:
lead_dn_lst.append('查询不到')
def insert_excel():
sheet.range(f"A2:A{sheet.cells.last_cell.row}").value = None
sheet.range(f"B2:B{sheet.cells.last_cell.row}").value = None
sheet.range(f"C2:C{sheet.cells.last_cell.row}").value = None
sheet.range(f"D2:D{sheet.cells.last_cell.row}").value = None
sheet.range(f"E2:E{sheet.cells.last_cell.row}").value = None
sheet.range(f"F2:F{sheet.cells.last_cell.row}").value = None
sheet.range(f"G2:G{sheet.cells.last_cell.row}").value = None
sheet.range(f"H2:H{sheet.cells.last_cell.row}").value = None
sheet.range(f"I2:I{sheet.cells.last_cell.row}").value = None
sheet.range(f"J2:J{sheet.cells.last_cell.row}").value = None
for own, lend in zip(own_dn_lst, lead_dn_lst):
timeee = datetime.datetime.now()
time_n = timeee.strftime('%m-%d %H:%M:%S')
attr = {'manager': [(MODIFY_REPLACE, [lend])]}
stata = c.modify(own, attr)
if stata == True:
stata_lst.append('修改成功')
time_now.append(time_n)
elif stata == False:
stata_lst.append('修改失败')
time_now.append(time_n)
for d in own_id:
try:
c.search(dn, f"(sAMAccountName={d})", SUBTREE, attributes=['name'])
oname = c.response[0]['attributes']['name']
onname = oname.split('(')[0]
own_name_lst.append(onname)
except:
own_name_lst.append('查不到姓名')
try:
c.search(dn, f"(sAMAccountName={d})", SUBTREE, attributes=['mail'])
mail = c.response[0]['attributes']['mail']
own_mail_lst.append(mail)
except:
own_mail_lst.append('查不到邮箱')
try:
c.search(dn, f"(sAMAccountName={d})", SUBTREE, attributes=['userAccountControl'])
userAccountControl = c.response[0]['attributes']['userAccountControl']
if userAccountControl == 512:
user_st = '账户正常'
own_state_lst.append(user_st)
elif userAccountControl == 514:
user_st = '账户禁用了(离职)'
own_state_lst.append(user_st)
notice2.append(f'工号: {d} 状态异常,请确认是否在职?')
elif userAccountControl == 544:
user_st = '账户正常,下次登录需要设置密码'
own_state_lst.append(user_st)
elif userAccountControl == 532480:
user_st = '域控制器(可进行Kerberos委派)'
own_state_lst.append(user_st)
elif userAccountControl == 66048:
user_st = '账户正常,密码永不过期'
own_state_lst.append(user_st)
elif userAccountControl == 2048:
user_st = '信任域间可信账户'
own_state_lst.append(user_st)
elif userAccountControl == 4096:
user_st = '工作站计算机'
own_state_lst.append(user_st)
elif userAccountControl == 16:
user_st = '账户被锁定,5分钟后会自动解锁'
own_state_lst.append(user_st)
elif userAccountControl == 262656:
user_st = '需要智能卡登录'
own_state_lst.append(user_st)
elif userAccountControl == 546:
user_st = '账户被禁用'
own_state_lst.append(user_st)
notice2.append(f'工号: {d} 状态异常,请确认是否在职?')
else:
user_st = 'None'
own_state_lst.append(user_st)
except:
user_st = '没有账户(离职)'
own_state_lst.append(user_st)
notice2.append(f'工号: {d} 状态异常,请确认是否在职?')
for dd in leader_id:
try:
c.search(dn, f"(sAMAccountName={dd})", SUBTREE, attributes=['name'])
name = c.response[0]['attributes']['name']
nname = name.split('(')[0]
lead_name_lst.append(nname)
except:
lead_name_lst.append('查不到姓名')
try:
c.search(dn, f"(sAMAccountName={dd})", SUBTREE, attributes=['mail'])
mail = c.response[0]['attributes']['mail']
lead_mail_lst.append(mail)
except:
lead_mail_lst.append('查不到邮箱')
try:
c.search(dn, f"(sAMAccountName={dd})", SUBTREE, attributes=['userAccountControl'])
userAccountControl = c.response[0]['attributes']['userAccountControl']
if userAccountControl == 512:
user_st = '账户正常'
lead_state_lst.append(user_st)
elif userAccountControl == 514:
user_st = '账户禁用了(离职)'
lead_state_lst.append(user_st)
notice2.append(f'工号: {dd} 状态异常,请确认是否在职?')
elif userAccountControl == 544:
user_st = '账户正常,下次登录需要设置密码'
lead_state_lst.append(user_st)
elif userAccountControl == 532480:
user_st = '域控制器(可进行Kerberos委派)'
lead_state_lst.append(user_st)
elif userAccountControl == 66048:
user_st = '账户正常,密码永不过期'
lead_state_lst.append(user_st)
elif userAccountControl == 2048:
user_st = '信任域间可信账户'
lead_state_lst.append(user_st)
elif userAccountControl == 4096:
user_st = '工作站计算机'
lead_state_lst.append(user_st)
elif userAccountControl == 16:
user_st = '账户被锁定,5分钟后会自动解锁'
lead_state_lst.append(user_st)
elif userAccountControl == 262656:
user_st = '需要智能卡登录'
lead_state_lst.append(user_st)
elif userAccountControl == 546:
user_st = '账户被禁用'
lead_state_lst.append(user_st)
notice2.append(f'工号: {dd} 状态异常,请确认是否在职?')
else:
user_st = 'None'
lead_state_lst.append(user_st)
except:
user_st = '没有账户(离职)'
lead_state_lst.append(user_st)
notice2.append(f'工号: {dd} 状态异常,请确认是否在职?')
sheet.range('A2').options(transpose=True).value = own_id
sheet.range('B2').options(transpose=True).value = leader_id
sheet.range('C2').options(transpose=True).value = stata_lst
sheet.range('D2').options(transpose=True).value = own_name_lst
sheet.range('E2').options(transpose=True).value = lead_name_lst
sheet.range('F2').options(transpose=True).value = time_now
sheet.range('G2').options(transpose=True).value = own_mail_lst
sheet.range('H2').options(transpose=True).value = lead_mail_lst
sheet.range('I2').options(transpose=True).value = own_state_lst
sheet.range('J2').options(transpose=True).value = lead_state_lst
wb.save(path)
wb.close()
app.quit()
c.unbind()
def powershell_mail():
if len(notice2) >= 1:
aa = set(notice2)
data2 = '\n'.join(aa)
else:
data2 = None
ps_script = f'''$date = (Get-Date).ToString("yyyyMMdd")
#邮件正文
$smtpServer = "192.168.21.20"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtpUser = "System.Script"
$smtpPassword = "bbbb888()"
$smtp.Credentials = New-object System.Net.networkCredential($smtpUser,$smtpPassword)
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($user1)
$msg.Cc.Add($user2)
$msg.Cc.Add($user3)
$msg.Subject = "邮件仲裁指定主管账号_$date"
$Attachments=New-Object System.Net.Mail.Attachment("C:\\SCRIPTS\\更改仲裁主管表信息.xlsx") #创建附件
$msg.Attachments.add($Attachments)
#$msg.IsBodyHTML = $true
$msg.Body = "Hi Team
邮件仲裁指定主管账号请查看附件Excel明细.
\nNotice:\n\n{data2}"
$smtp.Send($msg)
$body = ""
'''
args = ['powershell', '-Command', ps_script]
subprocess.call(args)
if __name__ == '__main__':
is_excel_running = is_excel_running()
if is_excel_running:
kill_excel()
path = 'C:\\SCRIPTS\\更改仲裁主管表信息.xlsx'
app = xw.App(visible=False, add_book=False)
app.display_alerts = False
app.screen_updating = False
wb = app.books.open(path)
sheet = wb.sheets['更改仲裁主管']
own_id = []
leader_id = []
own_dn_lst = []
lead_dn_lst = []
stata_lst = []
own_name_lst = []
lead_name_lst = []
time_now = []
own_mail_lst = []
lead_mail_lst = []
own_state_lst = []
lead_state_lst = []
notice2 = []
ad_admin_username = 'bbbb\\aaaa'
ad_admin_password = '123456789'
server = Server('192.168.2.2', get_info=ALL, use_ssl=True)
c = Connection(server, user=ad_admin_username, password=ad_admin_password, auto_bind=True)
dn = 'DC=luxshare,DC=com,DC=cn'
open_excel()
find_dn()
insert_excel()
powershell_mail()
#!/usr/bin/env python
# Version: Python 3.11
from ldap3 import Server, Connection, ALL, MODIFY_REPLACE, SUBTREE
import xlwings as xw
import datetime
import psutil
import subprocess
import re
def is_excel_running():
for proc in psutil.process_iter([
'name']):
if proc.info['name'] == 'EXCEL.EXE':
return True
return False
def kill_excel():
pass
# WARNING: Decompyle incomplete
if is_excel_running():
kill_excel()
path = 'C:\\SCRIPTS\\更改仲裁主管表信息.xlsx'
app = xw.App(visible = False, add_book = False)
app.display_alerts = False
app.screen_updating = False
wb = app.books.open(path)
sheet = wb.sheets['更改仲裁主管']
own_id = []
leader_id = []
own_dn_lst = []
lead_dn_lst = []
stata_lst = []
own_name_lst = []
lead_name_lst = []
time_now = []
own_mail_lst = []
lead_mail_lst = []
own_state_lst = []
lead_state_lst = []
own_qx_lst = []
lead_qx_lst = []
notice = []
notice2 = []
quanxian_lst = [
'DDDOFFICE',
'DDDNST',
'DDDNYB',
'DDDMST',
'DDDMYB',
'DDDB-MST',
'DDDB-MYB',
'JJJJJCPC',
'DDDTST',
'DDDTYB',
'EEEEEEOA',
'DDDA-CPC',
'DDDB-CPC',
'JJJJJOffice',
'JJJJJMST',
'JJJJJMYB',
'JJJJJNST',
'JJJJJNYB',
'JJJJJORT',
'KKKKKKKKOA',
'LLLLLLLLOA',
'MMMMMMMMB0']
ad_admin_username = 'bbbb\\jses'
ad_admin_password = '123456789'
server = Server('192.168.2.2', get_info = ALL, use_ssl = True)
c = Connection(server, user = ad_admin_username, password = ad_admin_password, auto_bind = True)
dn = 'DC=bbbb,DC=com,DC=cn'
c.start_tls()
def open_excel():
AB_dict = { }
data_A = sheet.range('A2').expand('down').value
data_B = sheet.range('B2').expand('down').value
data_A_list = data_A()
data_B_list = data_B()
# WARNING: Decompyle incomplete
def find_dn():
pass
# WARNING: Decompyle incomplete
def insert_excel():
range_to_clear = sheet.range('A2:A' + str(sheet.cells.last_cell.row))
range_to_clear.value = None
range_to_clear2 = sheet.range('B2:B' + str(sheet.cells.last_cell.row))
range_to_clear2.value = None
range_to_clear3 = sheet.range('C2:C' + str(sheet.cells.last_cell.row))
range_to_clear3.value = None
range_to_clear4 = sheet.range('D2:D' + str(sheet.cells.last_cell.row))
range_to_clear4.value = None
range_to_clear5 = sheet.range('E2:E' + str(sheet.cells.last_cell.row))
range_to_clear5.value = None
range_to_clear6 = sheet.range('F2:F' + str(sheet.cells.last_cell.row))
range_to_clear6.value = None
range_to_clear7 = sheet.range('G2:G' + str(sheet.cells.last_cell.row))
range_to_clear7.value = None
range_to_clear8 = sheet.range('H2:H' + str(sheet.cells.last_cell.row))
range_to_clear8.value = None
range_to_clear9 = sheet.range('I2:I' + str(sheet.cells.last_cell.row))
range_to_clear9.value = None
range_to_clear10 = sheet.range('J2:J' + str(sheet.cells.last_cell.row))
range_to_clear10.value = None
range_to_clear11 = sheet.range('K2:K' + str(sheet.cells.last_cell.row))
range_to_clear11.value = None
range_to_clear12 = sheet.range('L2:L' + str(sheet.cells.last_cell.row))
range_to_clear12.value = None
# WARNING: Decompyle incomplete
def powershell_mail():
pass
# WARNING: Decompyle incomplete
if __name__ == '__main__':
open_excel()
find_dn()
insert_excel()
powershell_mail()
return None