【问题标题】:Grep specific domain and all subdomains from access.loggrep 特定域和 access.log 中的所有子域
【发布时间】:2013-11-19 14:50:53
【问题描述】:

我正在尝试从 Apache2 access.log grep 带有域的特定行。在我的 access.log 中,我有我所有的虚拟主机和不同的域。

cat/var/log/access.log:

www.something-else-domain.si:80 193.77.xxx. xxx - - [06/Nov/2013:12:21:45 +0100] "GET /path/to/dir/image.jpg HTTP/1.1" 304 - "www.something-else-domain.si/index.php" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"

www.domain.si:80 193.77.xxx. xxx - - [06/Nov/2013:12:21:45 +0100] "GET /path/to/dir/image. jpg HTTP/1.1" 304 - "www.domain.si/index.php" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"

domain.si:80 193.77.xxx. xxx - - [06/Nov/2013:12:21:45 +0100] "GET /path/to/dir/image. jpg HTTP/1.1" 304 - "www.domain.si/index.php" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"

我只想grep domain.si 和www.domain.si 和whatever.domain.si 而不是something-else-domain.si。我怎么能那样做?感谢您的帮助。

【问题讨论】:

    标签: apache bash logging grep


    【解决方案1】:
    egrep '^([^ ]*\.)?domain\.si' /var/log/access.log
    

    把它拆开:

    • ^ 是行首。
    • (xxx)? 是“匹配 xxx 或什么都没有”;在这种情况下,匹配:
      • 什么都没有,这是裸域名的情况(domain.si
      • [^ ]*\.,任何不是空格的字符串,后跟一个点。这与可选的 www.whatever. 部分匹配。
    • domain\.si 只匹配 domain.si 部分。

    ^ 的锚定以及“无空格”位确保您只匹配行首的内容(而不是像 GET /domain.si 这样的请求)。

    【讨论】:

      【解决方案2】:

      gnu awk 解决方案

      awk  '/www.domain$|domanin$/ {print $NF RS}' RS=".si"
      www.domain.si
      "www.domain.si
      "www.domain.si
      

      你的例子有问题。 url中不允许有空格

      【讨论】:

        猜你喜欢
        • 2018-07-20
        • 2020-06-04
        • 1970-01-01
        • 1970-01-01
        • 2014-04-24
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多