python使用Windows的wmic命令监控文件运行状况,如有异常发送邮件报警

作者:小胡要加油 时间:2022-01-13 09:48:25 

   使用Windows的wmic命令,获取可执行文件的运行状况、文件路径、PID,如果可执行文件挂掉,就重启并邮件告警。

      因为监控的可执行文件的文件名一样,不好区分,所以我使用文件的绝对路径为标准来判断是否正常运行,代码及详细解释如下:


# -*- 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 = '发送的邮箱地址'
 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 = "可执行文件的绝对路径"
fileName = '可执行文件名'
mailtext = '报警邮件内容'
mailsubject = '报警邮件标题'
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"可执行文件名")
 mailsend(mailtext,mailsubject)

1.get_pidWay函数:

输入file_name,返回文件路径、文件名、文件Pid的列表,用split函数和ept_list字符串使返回的列表变成[[文件路径,文件名,Pid],[文件路径,文件名,Pid]]这样的二维数组;

2.open_file函数:

使用win32api模块,类似在cmd中执行程序,打开指定的可执行文件;

3.mailsend函数:

发送邮件,我用的qq的smtp模块,在qq邮箱的设置里可以开启smtp端口;

username_send发送邮件的邮箱地址,password是开启smtp端口时弹出的字符串;

username_recv收邮件的邮箱地址;

在内网要采用smtplib.SMTP_SSL(mailserver)连接(其中mailserver= ‘smtp.qq.com'),使用smtp = smtplib.SMTP(mailserver,port=465)方式会报错:smtplib.SMTPServerDisconnected: Connection unexpectedly closed

4.主函数:

file_path :放置可执行文件的目录;

fileName:可执行文件的文件名;

for循环来判断file_path是否在我们 get_pidWay函数返回的列表中,从而知道可执行文件是否正常运行;

如果没有运行,pos = 0,则运行文件、发送邮件。

来源:https://www.cnblogs.com/hydd/p/13743897.html

标签:python,监控文件,windows,wmic,邮件报警
0
投稿

猜你喜欢

  • 九宫格基本布局

    2009-06-18 18:36:00
  • python try 异常处理(史上最全)

    2021-11-19 21:03:04
  • 如何用mysql自带的定时器定时执行sql(每天0点执行与间隔分/时执行)

    2024-01-16 01:58:31
  • Go语言学习之数组的用法详解

    2024-04-26 17:18:34
  • MySQL 8.0.19支持输入3次错误密码锁定账户功能(例子)

    2024-01-28 04:48:00
  • asp如何用WSH获取机器的IP配置信息?

    2010-06-13 14:39:00
  • python笔记之使用fillna()填充缺失值

    2023-12-22 19:53:49
  • Python 异步协程函数原理及实例详解

    2022-05-26 11:29:55
  • 详解mysql的limit经典用法及优化实例

    2024-01-15 19:45:25
  • CentOs7 64位 mysql 5.6.40源码安装过程

    2024-01-25 00:36:24
  • 使用xshell实现代理功能并navicat for MySQL 进行测试

    2024-01-23 19:59:05
  • Python绘制三角函数图(sin\\cos\\tan)并标注特定范围的例子

    2021-06-17 17:53:29
  • Flask-Mail用法实例分析

    2023-08-07 20:24:25
  • js弹出新窗口而不会被浏览器阻止的方法

    2010-04-06 12:38:00
  • 企业网站该怎么做?

    2009-06-29 16:11:00
  • Christopher Schmitt 谈学习CSS的益处

    2008-07-13 14:15:00
  • MySQL旧版本升级为新版本

    2009-02-26 15:44:00
  • python获取当前计算机cpu数量的方法

    2023-08-01 14:20:03
  • sql分类汇总及Select的自增长脚本

    2024-01-22 20:43:34
  • Python的os包与os.path模块的用法详情

    2023-06-04 05:52:48
  • asp之家 网络编程 m.aspxhome.com