Python监视进程创建情况和系统服务状态

(1)监视Windows系统中进程创建情况 

import wmi

c = wmi.WMI()

process_watcher = c.Win32_Process.watch_for('creation')

while True:

    try:

        new_process = process_watcher()

        proc_owner = '{0[0]}\\{0[1]}'.format(new_process.GetOwner())

        temp_creation_date = new_process.CreationDate

        creation_date = temp_creation_date[:4]

        for i in (4, 6):

            creation_date += '-'+temp_creation_date[i:i+2]

        creation_date += ' '

        for i in (8, 10, 12):

            creation_date += temp_creation_date[i:i+2]+':'

        creation_date = creation_date[:-1]

        executable = new_process.ExecutablePath

        cmdline = new_process.CommandLine

        pid = new_process.ProcessId

        parent_pid = new_process.ParentProcessId

        print('='*30)

        print('Process owner:'.ljust(18), proc_owner)

        print('Creation Time:'.ljust(18), str(creation_date))

        print('Executable:'.ljust(18), executable)

        print('Cmdline:'.ljust(18), cmdline)

        print('ProcessId:'.ljust(18), pid)

        print('Parent ProcessId:'.ljust(18), parent_pid)

    except:

        pass
(2)查看Windows系统中服务状态
import itertools

import wmi

def group(service):

    if service.State == 'Stopped':

        return 'Stopped'

    elif service.State == 'Running':

        return 'Running'

    else:

        return 'Others'

result = dict()

c = wmi.WMI()

for service in c.Win32_Service():

    state = service.State

    caption = service.Caption

    t = result.get(state,[])

    t.append(caption)

    result[state] = t

for state, captions in result.items():

    print('='*30)

    print(state)

    print('\n'.join(sorted(captions)))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值