windows使用python原生组件包获取系统日志信息
- #coding=utf8
- import sys
- import traceback
- import win32con
- import win32evtlog
- import win32evtlogutil
- import winerror
- try:
- from _utils.patrol2 import run_cmd, data_format, report_format
- except:
- print 'no module _utils'
- import platform
- import datetime,psutil
- def getAllEvents(server, logtypes,time_flag):
- """
- """
- if not server:
- serverName = "localhost"
- else:
- serverName = server
- for logtype in logtypes:
- result=getEventLogs(server, logtype,time_flag)
- return result
- # ----------------------------------------------------------------------
- def getEventLogs(server, logtype, time_flag,logPath=None):
- """
- Get the event logs from the specified machine according to the
- logtype (Example: Application) and save it to the appropriately
- named log file
- """
- print "Logging %s events" % logtype
- # log = codecs.open(logPath, encoding='utf-8', mode='w')
- # line_break = '-' * 80
- #
- # log.write("\n%s Log of %s Events\n" % (server, logtype))
- # log.write("Created: %s\n\n" % time.ctime())
- # log.write("\n" + line_break + "\n")
- # 读取本机的,system系统日志
- hand = win32evtlog.OpenEventLog(server, logtype)
- # 获取system日志的总行数
- total = win32evtlog.GetNumberOfEventLogRecords(hand)
- print "Total events in %s = %s" % (logtype, total)
- flags = win32evtlog.EVENTLOG_BACKWARDS_READ | win32evtlog.EVENTLOG_SEQUENTIAL_READ
- events = win32evtlog.ReadEventLog(hand, flags, 0)
- # 错误级别类型
- evt_dict = {win32con.EVENTLOG_AUDIT_FAILURE: 'EVENTLOG_AUDIT_FAILURE',
- win32con.EVENTLOG_AUDIT_SUCCESS: 'EVENTLOG_AUDIT_SUCCESS',
- win32con.EVENTLOG_INFORMATION_TYPE: 'EVENTLOG_INFORMATION_TYPE',
- win32con.EVENTLOG_WARNING_TYPE: 'EVENTLOG_WARNING_TYPE',
- win32con.EVENTLOG_ERROR_TYPE: 'EVENTLOG_ERROR_TYPE'}
- try:
- events = 1
- count=0
- while events:
- events = win32evtlog.ReadEventLog(hand, flags, 0)
- for ev_obj in events:
- the_time = ev_obj.TimeGenerated.Format() # '12/23/99 15:54:09'
- the_time=datetime.datetime.strptime(the_time, "%m/%d/%y %H:%M:%S")
- if the_time < time_flag:
- continue
- evt_id = str(winerror.HRESULT_CODE(ev_obj.EventID))
- computer = str(ev_obj.ComputerName)
- cat = ev_obj.EventCategory
- ## seconds=date2sec(the_time)
- record = ev_obj.RecordNumber
- msg = win32evtlogutil.SafeFormatMessage(ev_obj, logtype)
- source = str(ev_obj.SourceName)
- if not ev_obj.EventType in evt_dict.keys():
- evt_type = "unknown"
- else:
- evt_type = str(evt_dict[ev_obj.EventType])
- if evt_id=='4625':
- count+=1
- # log.write("Event Date/Time: %s\n" % the_time)
- # log.write("Event ID / Type: %s / %s\n" % (evt_id, evt_type))
- # log.write("Record #%s\n" % record)
- # log.write("Source: %s\n\n" % source)
- # log.write(msg)
- # log.write("\n\n")
- # log.write(line_break)
- # log.write("\n\n")
- return count
- except:
- print traceback.print_exc(sys.exc_info())
- sys.exit(1)
- def get_start_time():
- dt = datetime.datetime.fromtimestamp(psutil.boot_time())
- return dt
- if __name__ == "__main__":
- time_flag= get_start_time()
- print time_flag
- server = None # None = local machine
- logTypes = ["Security"]#"System", "Application",
- result=getAllEvents(server, logTypes,time_flag)
- if result==0:
- alert=0
- else:
- alert = 1
- hostname=platform.node()
- report=data_format('登录失败次数',result,alert)
- reports=report_format(hostname,report,is_json=True)
- print reports
windows使用python原生组件包获取系统日志信息的更多相关文章
- 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop
[源码下载] 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 作者:weba ...
- 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化
[源码下载] 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...
- python 面向对象编程、获取对象信息
面向对象与面向过程 参考链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0 ...
- 测试成长记录:python调adb无法获取设备信息bug记录
背景介绍: 一直在负责公司Android自动化的编写工作,采用的是uiautomator2,需要获取设备id来连接设备,就是 adb devices 问题描述: 之前一直用 subprocess.ch ...
- python+adb实现自动化获取手机信息
首先我们先看一下使用adb查看Android手机信息的指令 #获取手机名称NAME = 'adb shell getprop ro.product.model'#获取手机版本VERSION = 'ad ...
- 利用wireshark抓包获取cookie信息
以下是一些过滤规则: 1. 百度的cookie: http.cookie matches "BDUSS" 2. 博客园的cookie: http.cookie matches &q ...
- Python连接MySQL数据库获取数据绘制柱状图
一.Python通过pymysql包获取MySQL数据库中的数据(没有对应包的可以通过pip install pymysql 安装对应的包) import matplotlib.pyplot as p ...
- 微信小程序在组件中获取界面上的节点信息wx.createSelectorQuery
节点信息查询 API 可以用于获取节点属性.样式.在界面上的位置等信息. 最常见的用法是使用这个接口来查询某个节点的当前位置,以及界面的滚动位置. 示例代码: const query = wx.cre ...
- 小程序wx.getUserInfo获取用户信息方案介绍
问题模块 框架类型 问题类型 API/组件名称 终端类型 操作系统 微信版本 基础库版本 API和组件 - - - - 背景 小程序一个比较重要的能力就是获取用户信息,也就是使用 wx.g ...
随机推荐
- yml实例
producer.yml apiVersion: v1kind: Podmetadata:name: producer-consumerspec:containers:- image: busybox ...
- Elasticsearch利用cat api快速查看集群状态、内存、磁盘使用情况
使用场景 当Elasticsearch集群中有节点挂掉,我们可以去查看集群的日志信息查找错误,不过在查找错误日志之前,我们可以通过elasticsearch的cat api简单判断下各个节点的状态,包 ...
- android广播(内部类)使用
1.广播定义在一个单独的文件中 源码: public class MessageReceiver extends BroadcastReceiver{ @Override public void on ...
- 洛谷P1247 取火柴游戏
经典NIM游戏. 取XOR和即可. 注意输出方案时,找到大于异或和sum的,变为a[i] ^ sum即可. #include <cstdio> ; int a[N]; int main() ...
- 串口、COM口、TTL、RS-232、RS-485区别详解
1.串口.COM口是指的物理接口形式(硬件).而TTL.RS-232.RS-485是指的电平标准(电信号). 2.接设备的时候,一般只接GND RX TX.不会接Vcc或者+3.3v的电源线,避 ...
- Int2BinaryString.java
/****************************************************************************** * Compilation: javac ...
- Java面向对象----个人参考资料
Java面向对象 :什么是面向对象.类与对象.封装.构造方法.static关键字.继承.抽象类.接口.多态 一.什么是面向对象 1.面向过程思想 面向过程:(PO,Procedure Oriented ...
- CodeForces911D 逆序对
http://codeforces.com/problemset/problem/911/D 题意 给你一个长度为N的序列,有M次操作.每次翻转[l,r]的区间,每次操作后询问序列逆序对个数的奇偶性 ...
- spring-data-jpa初步认识
什么是spring data jpa? spirng data jpa是spring提供的一套简化JPA开发的框架,按照约定好的[方法命名规则]写dao层接口,就可以在不写接口实现的情况下,实现对数据 ...
- javascript 字符串的连接和截取
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...