Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可以用python实时获取windows日志吗?_Python_Windows_Logging - Fatal编程技术网

可以用python实时获取windows日志吗?

可以用python实时获取windows日志吗?,python,windows,logging,Python,Windows,Logging,我希望实时获取windows日志以进行分析。谷歌搜索出了一些东西,并提出了这个 import win32evtlog # requires pywin32 pre-installed server = 'localhost' # name of the target computer to get event logs logtype = 'System' # 'Application' # 'Security' System hand = win32evtlog.OpenEventLog(

我希望实时获取windows日志以进行分析。谷歌搜索出了一些东西,并提出了这个

import win32evtlog # requires pywin32 pre-installed

 server = 'localhost' # name of the target computer to get event logs
logtype = 'System' # 'Application' # 'Security' System
hand = win32evtlog.OpenEventLog(server,logtype)
flags = win32evtlog.EVENTLOG_BACKWARDS_READ|win32evtlog.EVENTLOG_SEQUENTIAL_READ
total = win32evtlog.GetNumberOfEventLogRecords(hand)

while True:
     events = win32evtlog.ReadEventLog(hand, flags,0)
     if events:
          for event in events: 
                print 'Event Category:', event.EventCategory
                print 'Time Generated:', event.TimeGenerated
                print 'Source Name:', event.SourceName
                print 'Event ID:', event.EventID
                print 'Event Type:', event.EventType
                data = event.StringInserts
                if data:
                    print 'Event Data:'
                    for msg in data:
                        print msg\n
它的作用是打印从开始到代码运行的那一刻的所有日志。有没有可能在有更新时持续监视并打印日志?

python文档是,但它们没有太大帮助,所以我还查看了

我不知道如何通过事件获取从
win32evtlog.ReadEventLog
接收到的对象,但该库允许呈现为XML,因此使用XML解析器,您应该能够提取所需的所有信息:

导入win32evtlog
导入pprint
导入系统
#订阅并记录“应用程序”事件
#要手动触发新事件,请打开管理控制台并键入:(将125替换为适合您的任何其他ID)
#eventcreate.exe/L“应用程序”/t警告/id 125/d“这是一个测试警告”
#如果不需要,event_上下文可以是'None',这只是为了演示它是如何工作的
event_context={“info”:“此对象始终传递给回调”}
#要侦听的事件日志源
事件\源='应用程序'
def新日志事件处理程序(原因、上下文、evt):
"""
记录新事件时调用。
原因-记录事件的原因?
context-事件处理程序注册的上下文
evt-事件句柄
"""
#只需打印一些有关活动的信息
打印('reason',reason',context',context',event handle',evt)
#将事件渲染为xml,也许有一种方法可以获取对象,但我没有找到它
打印('Rendered event:',win32evtlog.EvtRender(evt,win32evtlog.EvtRenderEventXml))
#空行分隔日志
打印('-')
#确保所有打印的文本现在都已打印到控制台
sys.stdout.flush()
返回0
#订阅未来活动
订阅=win32evtlog.EvtSubscribe(事件源,win32evtlog.evtsubscribetoutureevents,无,回调=新日志\事件\处理程序,上下文=事件\上下文,查询=无)
输出

reason 1 context {'info': 'this object is always passed to your callback'} event handle 1
Rendered event: <Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'><System><Provider Name='EventCreate'/><EventID Qualifiers='0'>125</EventID><Level>3</Level><Task>0</Task><Keywords>0x80000000000000</Keywords><TimeCreated SystemTime='2020-03-03T15:23:11.150209500Z'/><EventRecordID>1</EventRecordID><Channel>Application</Channel><Computer>mypc</Computer><Security UserID='guid'/></System><EventData><Data>This is a test warning</Data></EventData></Event>
 -
reason 1 context {'info': 'this object is always passed to your callback'} event handle 1
Rendered event: <Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'><System><Provider Name='EventCreate'/><EventID Qualifiers='0'>125</EventID><Level>3</Level><Task>0</Task><Keywords>0x80000000000000</Keywords><TimeCreated SystemTime='2020-03-03T15:23:17.876041700Z'/><EventRecordID>2</EventRecordID><Channel>Application</Channel><Computer>mypc</Computer><Security UserID='guid'/></System><EventData><Data>This is a test warning 2</Data></EventData></Event>
 -
reason 1 context {'info': 'this object is always passed to your callback'} event handle 1
Rendered event: <Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'><System><Provider Name='EventCreate'/><EventID Qualifiers='0'>125</EventID><Level>3</Level><Task>0</Task><Keywords>0x80000000000000</Keywords><TimeCreated SystemTime='2020-03-03T15:23:20.476312800Z'/><EventRecordID>3</EventRecordID><Channel>Application</Channel><Computer>mypc</Computer><Security UserID='guid'/></System><EventData><Data>This is a test warning 3</Data></EventData></Event>
 -
原因1上下文{'info':'此对象始终传递给回调'}事件句柄1
呈现事件:125300x800000000000001ApplicationMyCt这是一个测试警告
-
原因1上下文{'info':'此对象始终传递给回调'}事件句柄1
呈现事件:125300x800000000000002ApplicationMyCt这是一个测试警告2
-
原因1上下文{'info':'此对象始终传递给回调'}事件句柄1
呈现事件:125300x800000000000003ApplicationMyCt这是一个测试警告3
-

此方法可能正是您想要的:(我通过
帮助(win32evtlog)
发现了此方法,并正在寻找与订阅/注册事件等相关的内容)@nitzel您能详细说明一下您的陈述吗?我对这个领域比较陌生。你调用这个方法,其中一个参数是回调,这是一个函数,在记录新事件时调用。您必须自己计算出确切的参数,或者使用该方法寻找示例。这里有一些关于实际windows函数的更多信息:这表示要结束订阅,您需要调用
EvtClose
,返回值为
EvtSubscribe