dsf12313 2016-01-21 15:35
浏览 40
已采纳

preg_match_all从apache日志文件中计算ip地址

i am trying to grab ip address from apache log file and count unique ip

this apache log file have 22.22.22.22 more then 10 time and 125.245.25.25 not more then 10 time

i am trying to get ip only more then 10 time have in Apache log file

my PHP code

<?php 
    $iplist_file="/home/domain/public_html/iplist.txt";

    $iplist_file=file_get_contents($iplist_file);

    preg_match_all('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$iplist_file,$a);

    $count = count($a[0]);
    echo "<b>Number of ip</b> = " .$count."<p>";
?>

text file

22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
22.22.22.22 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
125.245.25.25 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
125.245.25.25 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
125.245.25.25 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
125.245.25.25 - - [21/Jan/2016:17:06:31 +0300] "POST 1.php HTTP/1.1" 200 632 "mydomain.com" "Mozilla/5.0 (PlayStation 4 3.15)
  • 写回答

1条回答 默认 最新

  • dongyuan1902 2016-01-21 15:52
    关注

    For get all IP from log you can use this very similar regex:

    preg_match_all('/^(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/m', $iplist_content, $matches);
    

    Flag m is here for multiline mode (http://php.net/manual/en/reference.pcre.pattern.modifiers.php), ?P<ip> for naming catching group - instead of $matches[1] i have $matches['ip'] (it is not necessary).

    As I say, in $matches['ip'] you have all IP founded in log. For count them you can use simple loop, or better you can use fnc array_count_values.

    When we put this all together, we get this:

    <?php
    
    $iplist_file = "/home/domain/public_html/iplist.txt";
    $iplist_content = file_get_contents($iplist_file);
    
    preg_match_all('/^(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/m', $iplist_content, $matches);
    
    foreach (array_count_values($matches['ip']) as $ip => $count) {
        print $ip . ': ' . $count . '<br>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)