一个非常简单的cef解析器。

pycef的Python项目详细描述


#Pycef公司 python 2/3的一个非常简单的cef解析器

我最初写这个是因为我找不到很多优秀的python cef解析器。我确实找到了[苏轼的一个](https://github.com/sooshie/cef_parser)让我开始(谢谢分享,先生!),但我选择了自己制作。

函数parse接受一个包含单个cef记录的字符串,并返回一个dict,该dict包含[cef format documentation](https://www.protect724.hpe.com/docs/DOC-1072)中定义的以下键:

  • 简化版
  • 设备供应商
  • 设备版本
  • 设备事件ClassID
  • 设备名
  • 设备严重性

如果在“extensions”部分中有任何key=value对(面对它,几乎每个cef记录都有这些),它们也将在dict中,dict密钥名与cef记录的密钥名相同。如果它不能识别任何cef数据,parse函数将返回None

##示例用法 分析格式良好的CEF记录

>>> import pycef
>>> cef = 'CEF:0|pycef|python CEF tests|1|2|Test event 1|3| field1=value1 field2=value2 field3=value3'
>>> d = pycef.parse(cef)
>>> d
{'DeviceVendor': 'pycef', 'DeviceProduct': 'python CEF tests', 'DeviceVersion': '1', 'DeviceEventClassID': '2', 'DeviceName': 'Test event 1', 'DeviceSeverity': '3', 'CEFVersion': '0', 'field1': 'value1', 'field2': 'value2', 'field3': 'value3'}

从前端带有头垃圾邮件的源解析一行cef(注意:这并不像示例中那样特定于syslog头。解析器只在找到“cef:0”的地方启动:

>>> import pycef
>>> cef_syslog = 'Nov 16 21:24:18 arcsightfwd.davidbianco.io CEF:0|pycef|python CEF tests|1|2|Test event 1|3| field1=value1 field2=value2 field3=value3'
>>> d = pycef.parse(cef_syslog)
>>> d
{'DeviceVendor': 'pycef', 'DeviceProduct': 'python CEF tests', 'DeviceVersion': '1', 'DeviceEventClassID': '2', 'DeviceName': 'Test event 1', 'DeviceSeverity': '3', 'CEFVersion': '0', 'field1': 'value1', 'field2': 'value2', 'field3': 'value3'}

##日志记录 Pycef使用标准的pythonlogging模块。默认情况下,您不会看到任何日志,但可以在自己的应用程序中轻松配置它们。下面是一个示例:

import logging

# We log with the name ‘pycef’ logger = logging.getLogger(‘pycef’)

# set log level to DEBUG to get the most verbose output logger.setLevel(logging.DEBUG)

ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s’) ch.setFormatter(formatter) logger.addHandler(ch)

# well-formatted CEF data will log the parsed values at DEBUG level cef = ‘CEF:0|pycef|python CEF tests|1|2|Test event 1|3| field1=value1 field2=value2 field3=value3’ d = pycef.parse(cef) 2018-11-23 08:49:39,827 - pycef - DEBUG - Returning values: {‘DeviceVendor’: ‘pycef’, ‘DeviceProduct’: ‘python CEF tests’, ‘DeviceVersion’: ‘1’, ‘DeviceEventClassID’: ‘2’, ‘DeviceName’: ‘Test event 1’, ‘DeviceSeverity’: ‘3’, ‘CEFVersion’: ‘0’, ‘field1’: ‘value1’, ‘field2’: ‘value2’, ‘field3’: ‘value3’}

# Parse errors in the data will log at WARNING level pycef.parse(‘kjlk’) 2018-11-23 08:47:42,853 - pycef - WARNING - Could not parse record. Is it valid CEF format?

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在点击JButton触发的进程仍在处理时更新JLabel?   try-catch为什么Java有嵌套的try语句?   java SSH命令执行失败,出现异常“net.schmizz.sshj.connection.ConnectionException:引发连接重置异常”   java在ApacheCamel的接口类解析器中,resolveMandatoryClass(字符串名称)有什么用途?   java如何在Eclipse远程调试器中找到有问题的线程?   java线程:containerlaunch退出代码127的异常   lambda左连接Java中的2个对象列表   Swift 2.0协议扩展和Java/C抽象类之间有区别吗?   安卓改造:使用GSON将JSON解析为多个Java对象   Spring中服务层的java角色(澄清)   html Java与网站的通信   Spring boot rest api是在不创建java类的情况下将getResultList()转换为映射以显示响应的最佳方法吗?   使用“getElementById”从javascript获取值到java   java如何在文本视图中以粗体和多色显示文本   java是设置TextView颜色的最有效方法