grep一个模式,匹配多个文件

时间:2013-01-17 21:05:33

标签: grep access-log

我在攻击后使用grep来分析我的日志文件。 通常喜欢那样

grep -F "POST /xxxxx.php" ./access-log

现在有人攻击了我的一些网站,但我不知道漏洞的位置,也不知道攻击者的IP地址是什么。现在我想找到一个ip-address,它向我的多个网站发出请求,例如:

abcde.com-log:123.123.123.123 - - [12/Jan/2013:08:41:08 +0100] "POST /xxxxx.php HTTP/1.1" 200 1234 "-" "-"

wxyz.com-log:123.123.123.123 - - [12/Jan/2013:08:41:08 +0100] "POST /xxxxx.php HTTP/1.1" 200 1234 "-" "-"

但是我不知道如何使用grep或其他unix工具来仅给我匹配,匹配的是多个日志文件。

1 个答案:

答案 0 :(得分:0)

假设您想要的IP地址是每个日志文件中显示为第一个字段的IP地址,请尝试以下操作:

awk '
   /POST \/xxxxx\.php/ {
      ip=$1
      if ( !(ipFilePairs[ip,FILENAME]++) ) {
         ipFileCnt[ip]++
         ipFileList[ip] = ipFileList[ip] " " FILENAME
      }
   }
   END {
      for (ip in ipFileCnt)
         if (ipFileCnt[ip] > 1)
            print ip ":" ipFileList[ip]
   }
' *.log