LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-20-2012, 02:46 PM   #1
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Rep: Reputation: 73
Get top 10 ips from an apache log


Hello, how would you guys get the top 10 of ips from an apache log based on how many times they appear?

for example we have ip 10.0.10.1 50 times, 192.168.10.1 38 times, and so on, plus a tones of other ips which got only 1-2 hits.

How would you write a script that will get the 10 ip addresses that access a certain website based on the apache logs?
 
Old 08-20-2012, 02:59 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
This can be done in a more robust fashion but here is an easy quick way. If this is something you are going to rely upon heavily look at re-writing it to perl, python or something that can handle it better.

Code:
#!/bin/bash

##First create a temp file that you can use to manipulate the IPs.

/bin/cut -f1 -d" " /var/log/httpd/access_log | sort -n > /tmp/iplist.txt

### Now we do the counting and sorting

for ip in $(/bin/cat /tmp/iplist.txt | uniq)
do
IPCOUNT=$(grep $ip /tmp/iplist.txt | wc -l)
echo "$IPCOUNT connections from $ip" >> /tmp/ipcount.txt
done

### Now we do the reporting by pulling from the file

/bin/sort -nr /tmp/ipcount.txt > /tmp/ipresults.txt
/usr/bin/head -n10 /tmp/ipresults.txt

/bin/rm -f /tmp/iplist.txt
/bin/rm -f /tmp/ipcount.txt
/bin/rm -f /tmp/ipresults.txt
 
Old 08-20-2012, 03:06 PM   #3
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Ahh, something like this I've done too, was thinking something more "better" or more "cleaner".

Thanks anyway!
 
Old 08-22-2012, 02:52 AM   #4
centos123
Member
 
Registered: Apr 2011
Posts: 397

Rep: Reputation: 16
Quote:
Originally Posted by Kustom42 View Post
### Now we do the reporting by pulling from the file

/bin/sort -nr /tmp/ipcount.txt > /tmp/ipresults.txt
/usr/bin/head -n10 /tmp/ipresults.txt

/bin/rm -f /tmp/iplist.txt
/bin/rm -f /tmp/ipcount.txt
/bin/rm -f /tmp/ipresults.txt

[/CODE]
this part remove txt file then how will we see when it doesnot exist?

and i tried but it not working

/bin/sort: open failed: /tmp/ipcount.txt: No such file or directory
 
Old 08-22-2012, 11:37 AM   #5
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Quote:
Originally Posted by centos123 View Post
this part remove txt file then how will we see when it doesnot exist?

and i tried but it not working

/bin/sort: open failed: /tmp/ipcount.txt: No such file or directory
I'm not sure I understand you, these are temporary text files created by the script, they should not already exist.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
transparent proxy with squid and iptables won't log IPs from lan kabuki Linux - Newbie 1 09-13-2010 05:18 PM
In Apache server, How to change log file location and log format for access log fil? since1993 Linux - Server 1 08-19-2009 04:14 PM
How can I grep/sed the ips only from /var/log/messages? abefroman Programming 1 07-31-2009 01:47 PM
Getting things straight: Apache, SSL, Multiple External IPs / Internal IPs robin.com.au Linux - Server 21 10-13-2007 11:39 PM
Blocking IPs log from honeyd crackyblue Linux - Enterprise 3 09-19-2007 10:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration