溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在python中利用wmic命令對(duì)文件的運(yùn)行狀況進(jìn)行監(jiān)控

發(fā)布時(shí)間:2021-01-30 14:05:15 來(lái)源:億速云 閱讀:233 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)怎么在python中利用wmic命令對(duì)文件的運(yùn)行狀況進(jìn)行監(jiān)控,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

# -*- 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ā)送郵件。

關(guān)于怎么在python中利用wmic命令對(duì)文件的運(yùn)行狀況進(jìn)行監(jiān)控就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI