python使用Windows的wmic命令監(jiān)控文件運(yùn)行狀況,如有異常發(fā)送郵件報(bào)警
使用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 os
import win32api
import smtplib
from email.mime.text import MIMEText
def 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 = 0
for i in range(len(exe_info)):
if file_path in exe_info[i][0]:
pos = 1
else:
pass
if pos == 1:
pass
else:
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模塊,類似在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)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python數(shù)據(jù)分析之pandas比較操作
比較操作是很簡(jiǎn)單的基礎(chǔ)知識(shí),不過(guò)Pandas中的比較操作有一些特殊的點(diǎn),本文介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助.需要的朋友可以參考下2021-05-05
TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法
這篇文章主要介紹了TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
在Python中通過(guò)threshold創(chuàng)建mask方式
今天小編就為大家分享一篇在Python中通過(guò)threshold創(chuàng)建mask方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Python利用Rasa框架和SMTPlib庫(kù)實(shí)現(xiàn)郵件回復(fù)助手
在現(xiàn)代辦公場(chǎng)景中,處理大量郵件是一項(xiàng)既耗時(shí)又容易出錯(cuò)的任務(wù),本文將詳細(xì)介紹如何使用Python的Rasa框架和SMTPlib庫(kù)建一個(gè)智能的郵件自動(dòng)回復(fù)助手,感興趣的可以了解下2025-04-04
python日期與時(shí)間戳的各種轉(zhuǎn)換示例
這篇文章主要介紹了python日期與時(shí)間戳的各種轉(zhuǎn)換示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

