{"id":323,"date":"2010-08-26T05:34:17","date_gmt":"2010-08-26T09:34:17","guid":{"rendered":"http:\/\/www.xavignu.com\/?p=323"},"modified":"2011-02-07T01:24:17","modified_gmt":"2011-02-07T06:24:17","slug":"apache-visits-with-perl","status":"publish","type":"post","link":"https:\/\/www.xavignu.com\/?p=323","title":{"rendered":"Apache visits with Perl"},"content":{"rendered":"<p>Here is another <a href=\"http:\/\/www.perl.org\" target=\"_blank\">Perl<\/a> script. This one given an <a href=\"http:\/\/www.apache.org\" target=\"_blank\">Apache<\/a> log file will tell you the number of hits per IP. Here is the code.<br \/>\n[perl]<br \/>\n#!\/usr\/bin\/perl -w<\/p>\n<p>use strict;<br \/>\nuse diagnostics;<\/p>\n<p>if( $#ARGV != 0 ){<br \/>\n        print &quot;Usage: .\/parser.pl &amp;lt;logfile&amp;gt;\\n&quot;;<br \/>\n        exit 1; }<\/p>\n<p># Opening logfile<br \/>\nopen FILE, &quot;$ARGV[0]&quot; or die $!;<\/p>\n<p># Defining variables<br \/>\nmy @lines = &amp;lt;FILE&amp;gt;;<br \/>\nmy @ip;<br \/>\nmy $items = scalar(@lines);<br \/>\nmy ($i, $value);<br \/>\nmy %hits;<\/p>\n<p># Removing IP addresses from logfile and counting the hits per IP.<br \/>\nfor ($i = 0; $i &amp;lt; $items; $i++)<br \/>\n        { @ip = split (\/ \/, $lines[$i]);<br \/>\n        if (exists $hits{$ip[0]}) {<br \/>\n                $hits{$ip[0]} = $hits{$ip[0]}+1;<br \/>\n                chomp($hits{$ip[0]}); }<br \/>\n        else {  $hits{$ip[0]} = 1; }<br \/>\n        }<\/p>\n<p># Ordering and printing the hits per IP.<br \/>\nforeach $value (sort {&quot;$hits{$b}&quot; &amp;lt;=&amp;gt; &quot;$hits{$a}&quot; } keys %hits)<br \/>\n        { print &quot;$value made $hits{$value} hits.\\n&quot;; }<br \/>\n[\/perl]<br \/>\nHere is an usage example.<\/p>\n<pre>\r\nxavi@debianserver:~\/Perl$ .\/parser.pl \/tmp\/apachelog \r\n12.130.86.9 made 106 hits.\r\n208.111.39.192 made 89 hits.\r\n67.195.114.231 made 42 hits.\r\n98.14.22.97 made 27 hits.\r\n208.54.45.78 made 17 hits.\r\n213.5.71.12 made 16 hits.\r\n199.106.237.37 made 15 hits.\r\n66.249.67.212 made 15 hits.\r\n208.54.45.62 made 12 hits.\r\n87.250.252.242 made 9 hits.\r\n174.121.74.234 made 6 hits.\r\n188.187.102.74 made 6 hits.\r\n208.54.45.72 made 5 hits.\r\n64.40.121.184 made 4 hits.\r\n206.196.125.114 made 3 hits.\r\n207.46.13.94 made 2 hits.\r\n220.181.146.169 made 2 hits.\r\n142.166.170.101 made 2 hits.\r\n220.181.94.225 made 2 hits.\r\n142.166.170.100 made 2 hits.\r\n65.52.108.60 made 2 hits.\r\n207.46.199.182 made 2 hits.\r\n207.46.13.101 made 2 hits.\r\n208.54.45.56 made 2 hits.\r\n209.2.233.223 made 1 hits.\r\n67.210.218.102 made 1 hits.\r\n66.249.67.66 made 1 hits.\r\n220.181.7.54 made 1 hits.\r\n207.46.199.199 made 1 hits.\r\n<\/pre>\n<p>As usual, suggestions are always welcome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is another Perl script. This one given an Apache log file will tell you the number of hits per IP. Here is the code. [perl] #!\/usr\/bin\/perl -w use strict; use diagnostics; if( $#ARGV != 0 ){ print &quot;Usage: .\/parser.pl &amp;lt;logfile&amp;gt;\\n&quot;; exit 1; } # Opening logfile open FILE, &quot;$ARGV[0]&quot; or die $!; # Defining [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[3],"tags":[20,22,6,23,50,70],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/pTQgt-5d","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/323"}],"collection":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=323"}],"version-history":[{"count":0,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/323\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}