怎么在python中利用wmic命令对文件的运行状况进行监控

发布时间:2021-01-30 14:05:15 作者:Leah
来源:亿速云 阅读:224

这篇文章将为大家详细讲解有关怎么在python中利用wmic命令对文件的运行状况进行监控,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

# -*- 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,则运行文件、发送邮件。

关于怎么在python中利用wmic命令对文件的运行状况进行监控就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 怎么在python中利用SVD对图像进行压缩
  2. 怎么在Python 3.6 中利用pdfminer对pdf文件进行解析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python wmic

上一篇:idea中yml文件图标提示失效如何解决

下一篇:怎么在java后端中生成一个pdf模板

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》