代码之家  ›  专栏  ›  技术社区  ›  Anurag

读取日志文件并获取两个日期之间的条目

  •  0
  • Anurag  · 技术社区  · 8 年前

    我想从访问日志文件中提取一些与关键字匹配且在两个日期之间的信息。例如,我想查找两个日期之间包含文本“passwd”的日志条目。目前,我正在使用以下命令,但没有得到正确的结果:

    fgrep "passwd" * | awk '$4 >= "[20/Aug/2017" && $4 <= "[22/Aug/2017"'
    

    日期格式为[2017年2月22日:17:28:42+0000]。

    extract data from log file in specified range of time 但并不完全了解如何使用它。

    编辑:

    Following are the example entries of the access log files,
    xxx-access_log:xx.xx.xx.xx - - [22/Feb/2017:17:30:02 +0000] "GET /cms/usr/extensions/get_tree.inc.php?GLOBALS[root_path]=/etc/passwd%00 HTTP/1.1" 404 39798
    xxx-access_log:xx.xx.xx.xx - - [22/Feb/2017:17:31:12 +0000] "GET /cgi-bin/libs/smarty_ajax/index.php?_=&f=update_intro&page=../../../../../../../../../../../../../../../../../../etc/passwd%00 HTTP/1.1" 404 30083
    xxx-access_log:xx.xx.xx.xx - - [22/Feb/2017:17:31:19 +0000] "GET /download/libs/smarty_ajax/index.php?_=&f=update_intro&page=../../../../../../../../../../../../../../../../../../etc/passwd%00 HTTP/1.1" 404 27982
    xxx-access_log:xx.xx.xx.xx - - [22/Feb/2017:17:31:24 +0000] "GET /sites/libs/smarty_ajax/index.php?_=&f=update_intro&page=../../../../../../../../../../../../../../../../../../etc/passwd%00 HTTP/1.1" 404 35256
    xxx-access_log:xx.xx.xx.xx - - [22/Feb/2017:17:28:32 +0000] "GET /modx/manager/media/browser/mcpuk/connectors/php/Commands/Thumbnail.php?base_path=/etc/passwd%00 HTTP/1.1" 404 6956
    xxx-access_log:xx.xx.xx.xx - - [22/Feb/2017:17:28:42 +0000] "GET /modx/manager/media/browser/mcpuk/connectors/php/Commands/Thumbnail.php?base_path=/etc/passwd%00 HTTP/1.1" 404 6956
    

    提前感谢您的帮助!

    1 回复  |  直到 8 年前
        1
  •  1
  •   hymie    8 年前

    如果您知道日志文件中出现的两个特定字符串,则将使用您引用的链接。该命令将搜索第一个字符串并显示所有行,直到找到第二个字符串,然后停止。

    在你的情况下,如果你想要通用的日期操作,你最好使用 perl 和一个日期/时间模块。大多数(如果不是全部的话)都有内置的日期比较例程,其中许多都会以几乎任何可以想象的格式记录日期。。。而那些通常不提供指定日期格式的功能。