#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原生组件包获取系统日志信息的更多相关文章

  1. 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop

    [源码下载] 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 作者:weba ...

  2. 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化

    [源码下载] 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  3. python 面向对象编程、获取对象信息

    面向对象与面向过程 参考链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0 ...

  4. 测试成长记录:python调adb无法获取设备信息bug记录

    背景介绍: 一直在负责公司Android自动化的编写工作,采用的是uiautomator2,需要获取设备id来连接设备,就是 adb devices 问题描述: 之前一直用 subprocess.ch ...

  5. python+adb实现自动化获取手机信息

    首先我们先看一下使用adb查看Android手机信息的指令 #获取手机名称NAME = 'adb shell getprop ro.product.model'#获取手机版本VERSION = 'ad ...

  6. 利用wireshark抓包获取cookie信息

    以下是一些过滤规则: 1. 百度的cookie: http.cookie matches "BDUSS" 2. 博客园的cookie: http.cookie matches &q ...

  7. Python连接MySQL数据库获取数据绘制柱状图

    一.Python通过pymysql包获取MySQL数据库中的数据(没有对应包的可以通过pip install pymysql 安装对应的包) import matplotlib.pyplot as p ...

  8. 微信小程序在组件中获取界面上的节点信息wx.createSelectorQuery

    节点信息查询 API 可以用于获取节点属性.样式.在界面上的位置等信息. 最常见的用法是使用这个接口来查询某个节点的当前位置,以及界面的滚动位置. 示例代码: const query = wx.cre ...

  9. 小程序wx.getUserInfo获取用户信息方案介绍

    问题模块 框架类型 问题类型 API/组件名称 终端类型 操作系统 微信版本 基础库版本 API和组件 - -   - -     背景 小程序一个比较重要的能力就是获取用户信息,也就是使用 wx.g ...

随机推荐

  1. PKUWC 2019 记

     “连剑都插在了地上,可是我不应该就这么承认失败,想要到达山顶的人,不应该在山脚下就倒下啊” Day -5 (2019.1.15) 学考结束了,文化课暂停一段.早上飞机前往中山纪念中学.纪中好大呀,果 ...

  2. Linux中如何运行.AppImage文件

    今天在GitHub上发现了一个upterm( https://github.com/railsware/upterm )的开源项目,和其他的并不太一样,对于Linux发行版本只提供了AppImage的 ...

  3. 【洛谷P1073】最优贸易

    题目大意:给定一个 N 个点,M 条边(存在反向边)的有向图,点有点权,求一条从 1 到 N 的路径上,任意选出两个点 p,q (p 在前,q在后),两点点权的差值最大. 根据最短路的 dp 思想,可 ...

  4. javascript面向对象精要第二章函数整理精要

  5. python3之rabbitMQ

    1.RabbitMQ介绍 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息 ...

  6. plink, vcftool计算等位基因频率(allele frequency,vcf)

    计算等位基因频率有两种方式,第一种用vcftool计算: /path/to/vcftools --vcf file.vcf --freq --chr 1 --out filefreq 很简单的一个命令 ...

  7. Solr7.1---Getting Start

    目前最新版的Solr是7.1.0 有个我还不清楚的地方是,为何5.5.X或者6.6.X版本还在更新,给我的感觉好像每一个大版本都有自己的维护团队.不管了. 首先-系统要求 JDK1.8+ 解压Solr ...

  8. cookie、locakstorage、sessionstorage的区别

      cookie localstorage sessionstorage 数据的生命周期 可以设置失效时间,一般默认为浏览器关闭后 若不被清除,则永久保存 存在于一次会话中,刷新页面数据仍然存在,但关 ...

  9. 高效的SQLSERVER分页查询

    Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...

  10. 面向对象【day08】:反射(五)

    本节内容 概述 反射函数 综合使用 一.概述 反射我们以后会经常用到,这个东西实现了动态的装配,通过字符串来反射类中的属性和方法 二.反射函数 2.1 hasarttr(obj,name_str) 作 ...