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

  • Post category:Python

下面是使用Python监控文件运行状况并发送邮件报警的完整攻略:

一、WMIC命令简介

WMIC是Windows自带的命令行工具,可以用来获取操作系统信息、进程信息、服务信息,以及监控系统硬件、软件等。我们可以使用Python中的os模块来执行WMIC命令。

二、WMIC监控文件运行状况

可以使用WMIC命令来监控文件的运行状况,具体命令如下:

wmic process where "name='filename.exe'" get processid,executablepath,commandline,creationdate

其中,filename.exe为要监控的文件名。以上命令获取了filename.exe进程的进程ID、可执行文件路径、命令行参数以及进程创建时间,可以根据实际需求获取更多信息。

三、Python代码实现

接下来,我们使用Python来实现监控文件运行状况并发送邮件报警的功能。

1. 导入模块

在Python代码中,我们需要导入os模块来执行WMIC命令,同时还需要导入time模块来定时执行命令,smtplib及其子模块MIMEText来发送邮件报警。

import os
import time
import smtplib
from email.mime.text import MIMEText

2. 配置邮件参数

在代码中,需要配置发件人、收件人、邮件服务器地址、STMP账号和密码等参数。对于密码等敏感信息,建议采用加密处理。

# 配置邮件参数
from_addr = 'example@163.com'
password = 'example_password'
to_addr = 'example2@qq.com'
smtp_server = 'smtp.163.com'
smtp_port = 25

3. 定时执行WMIC命令并分析结果

在代码中,我们使用一个死循环来定时执行WMIC命令,然后根据执行结果判断是否需要发送邮件报警。其中,定时间隔可以根据实际需求进行设置。

while True:
    # 调用WMIC命令获取进程信息
    cmd = 'wmic process where "name=\'filename.exe\'" get processid,executablepath,commandline,creationdate'
    result = os.popen(cmd).read()
    # 如果获取到了包含指定文件名的进程,则认为运行正常
    if 'filename.exe' in result:
        print('[{}] Running normally.'.format(time.strftime('%Y-%m-%d %H:%M:%S')))
    # 否则,发送邮件报警
    else:
        print('[{}] Abnormal running detected. Sending email...'.format(time.strftime('%Y-%m-%d %H:%M:%S')))
        # 邮件内容
        msg = MIMEText('Abnormal running detected.')
        # 邮件主题
        msg['Subject'] = 'File abnormal running detected'
        # 发件人
        msg['From'] = 'Admin <{}>'.format(from_addr)
        # 收件人
        msg['To'] = to_addr
        # 邮件服务器地址
        server = smtplib.SMTP(smtp_server, smtp_port)
        # 登录SMTP服务器
        server.login(from_addr, password)
        # 发送邮件
        server.sendmail(from_addr, [to_addr], msg.as_string())
        # 退出SMTP服务器
        server.quit()
        print('[{}] Email sent.'.format(time.strftime('%Y-%m-%d %H:%M:%S')))
    # 等待指定时间后继续监控
    time.sleep(60)

在上面的代码中,我们使用os.popen()函数来执行WMIC命令,并将执行结果保存在result变量中。然后判断结果中是否包含指定的文件名,如果包含,就认为文件正常运行;否则,就发送邮件报警。在发送邮件时,我们使用MIMEText类型来创建邮件正文,并设置邮件主题、收件人和发件人等信息。最后,我们借助SMTP服务器来发送邮件报警。

4. 示例1:监控FileZilla Server运行状况

以FileZilla Server为例,我们来演示如何使用WMIC和Python来监控其运行状况并发送邮件报警。

首先,使用WMIC命令可以获取到FileZilla Server进程的信息,具体命令如下:

wmic process where "name='FileZilla server.exe'" get processid,executablepath,commandline,creationdate

然后,我们使用Python代码来实现定时执行WMIC命令,并根据结果来发送邮件报警的功能。完整代码如下:

import os
import time
import smtplib
from email.mime.text import MIMEText

# 发件人、收件人、邮件服务器地址、STMP账号和密码等参数
from_addr = 'example@163.com'
password = 'example_password'
to_addr = 'example2@qq.com'
smtp_server = 'smtp.163.com'
smtp_port = 25

while True:
    # 调用WMIC命令获取进程信息
    cmd = 'wmic process where "name=\'FileZilla server.exe\'" get processid,executablepath,commandline,creationdate'
    result = os.popen(cmd).read()
    # 如果获取到了包含指定文件名的进程,则认为运行正常
    if 'FileZilla server.exe' in result:
        print('[{}] Running normally.'.format(time.strftime('%Y-%m-%d %H:%M:%S')))
    # 否则,发送邮件报警
    else:
        print('[{}] Abnormal running detected. Sending email...'.format(time.strftime('%Y-%m-%d %H:%M:%S')))
        # 邮件内容
        msg = MIMEText('Abnormal running detected.')
        # 邮件主题
        msg['Subject'] = 'FileZilla Server abnormal running detected'
        # 发件人
        msg['From'] = 'Admin <{}>'.format(from_addr)
        # 收件人
        msg['To'] = to_addr
        # 邮件服务器地址
        server = smtplib.SMTP(smtp_server, smtp_port)
        # 登录SMTP服务器
        server.login(from_addr, password)
        # 发送邮件
        server.sendmail(from_addr, [to_addr], msg.as_string())
        # 退出SMTP服务器
        server.quit()
        print('[{}] Email sent.'.format(time.strftime('%Y-%m-%d %H:%M:%S')))
    # 等待指定时间后继续监控
    time.sleep(60)

在上面的代码中,我们使用FileZilla server.exe作为要监控的文件名进行测试。

5. 示例2:监控IIS运行状况

除了FileZilla Server,我们也可以利用WMIC和Python来监控IIS运行状况并发送邮件报警。

监控IIS的命令如下:

wmic process where "name='w3wp.exe'" get processid,executablepath,commandline,creationdate

IIS的进程名为w3wp.exe,我们可以根据实际情况进行调整。Python代码与FileZilla Server的监控实现方式类似,这里不再赘述。