国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python使用Windows的wmic命令監(jiān)控文件運(yùn)行狀況,如有異常發(fā)送郵件報(bào)警

瀏覽:174日期:2022-06-28 17:12:41

  使用Windows的wmic命令,獲取可執(zhí)行文件的運(yùn)行狀況、文件路徑、PID,如果可執(zhí)行文件掛掉,就重啟并郵件告警。

因?yàn)楸O(jiān)控的可執(zhí)行文件的文件名一樣,不好區(qū)分,所以我使用文件的絕對(duì)路徑為標(biāo)準(zhǔn)來(lái)判斷是否正常運(yùn)行,代碼及詳細(xì)解釋如下:

# -*- coding: utf-8 -*- import osimport win32apiimport smtplibfrom email.mime.text import MIMETextdef get_pidWay(file_name): ept_list = [] temp_list = [] pid_way = os.popen('wmic process where name=’' + file_name + '’ get processid,executablepath,name').readlines() for j in pid_way: temp_list.append(j.split()) while ept_list in temp_list: temp_list.remove(ept_list) return(temp_list) def open_file(filePath): win32api.ShellExecute(0, ’open’, filePath, ’’,’’,1)def mailsend (mailtext,mailsubject): mailserver = 'smtp.qq.com' username_send = ’發(fā)送的郵箱地址’ password = ’密碼’ username_recv = ’接收的郵箱地址’ mail = MIMEText(mailtext) mail[’Subject’] = mailsubject mail[’From’] = username_send mail[’To’] = username_recv smtp = smtplib.SMTP_SSL(mailserver) smtp.login(username_send,password) smtp.sendmail(username_send,username_recv,mail.as_string()) smtp.quit() print (’success’) file_path = '可執(zhí)行文件的絕對(duì)路徑'fileName = ’可執(zhí)行文件名’mailtext = ’報(bào)警郵件內(nèi)容’mailsubject = ’報(bào)警郵件標(biāo)題’exe_info = get_pidWay(fileName)pos = 0for i in range(len(exe_info)): if file_path in exe_info[i][0]: pos = 1 else: passif pos == 1: passelse: open_file(r'可執(zhí)行文件名') mailsend(mailtext,mailsubject)1.get_pidWay函數(shù):

輸入file_name,返回文件路徑、文件名、文件Pid的列表,用split函數(shù)和ept_list字符串使返回的列表變成[[文件路徑,文件名,Pid],[文件路徑,文件名,Pid]]這樣的二維數(shù)組;

2.open_file函數(shù):

使用win32api模塊,類(lèi)似在cmd中執(zhí)行程序,打開(kāi)指定的可執(zhí)行文件;

3.mailsend函數(shù):

發(fā)送郵件,我用的qq的smtp模塊,在qq郵箱的設(shè)置里可以開(kāi)啟smtp端口;

username_send發(fā)送郵件的郵箱地址,password是開(kāi)啟smtp端口時(shí)彈出的字符串;

username_recv收郵件的郵箱地址;

在內(nèi)網(wǎng)要采用smtplib.SMTP_SSL(mailserver)連接(其中mailserver= ‘smtp.qq.com’),使用smtp = smtplib.SMTP(mailserver,port=465)方式會(huì)報(bào)錯(cuò):smtplib.SMTPServerDisconnected: Connection unexpectedly closed

4.主函數(shù):

file_path :放置可執(zhí)行文件的目錄;

fileName:可執(zhí)行文件的文件名;

for循環(huán)來(lái)判斷file_path是否在我們 get_pidWay函數(shù)返回的列表中,從而知道可執(zhí)行文件是否正常運(yùn)行;

如果沒(méi)有運(yùn)行,pos = 0,則運(yùn)行文件、發(fā)送郵件。

以上就是python使用Windows的wmic命令監(jiān)控文件運(yùn)行狀況,如有異常發(fā)送郵件報(bào)警的詳細(xì)內(nèi)容,更多關(guān)于python wmic命令監(jiān)控文件運(yùn)行狀況的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
主站蜘蛛池模板: 遂川县| 怀仁县| 甘谷县| 隆回县| 沽源县| 枞阳县| 大同县| 井研县| 呼图壁县| 宣化县| 永新县| 巩义市| 册亨县| 财经| 缙云县| 庆元县| 运城市| 西盟| 娄底市| 称多县| 临高县| 濮阳县| 尉氏县| 黑山县| 青冈县| 时尚| 永平县| 方山县| 稻城县| 三河市| 济南市| 光山县| 吉木乃县| 郯城县| 资阳市| 大埔县| 南平市| 饶阳县| 定南县| 霍城县| 成武县|