Shell Script List All Top IP Address Accessing Apache / Lighttpd Web Server

by admin on July 24, 2009 · 0 comments

#!/bin/bash
# Shell Script To List All Top Hitting IP Address to your webserver.
# This may be useful to catch spammers and scrappers.
# -------------------------------------------------------------------------
# Copyright (c) 2004 nixCraft project <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
 
# where to store final report?
DEST=/var/www/reports/ips
 
# domain name
DOM=$1
 
# log file location
LOGFILE=/var/logs/httpd/$DOM/access.log
 
# die if no domain name given
[ $# -eq 0 ] && exit 1
 
# make dir
[ ! -d $DEST ] && mkdir -p $DEST
 
# ok, go though log file and create report
if [ -f $LOGFILE ]
then
	echo "Processing log for $DOM..."
	awk '{ print $1}' $LOGFILE | sort  | uniq -c  | sort -nr > $DEST/$DOM.txt
	echo "Report written to $DEST/$DOM.txt"
fi

How do I run this script?

Simply run it as follows:
./script nixcraft.com
Sample output (1st coloum is counter and 2nd is IP address):

  13687 72.30.87.116
   7416 66.249.71.138
   7402 66.249.71.140
   7261 66.249.71.139
   6510 74.86.49.130
   4879 67.195.37.159
   4121 66.90.104.20
   3958 93.158.144.27
   3262 122.172.49.89

You can block all spammers and content scrappers bots using Linux iptables or BSD pf firewall itself.

Previous post:

Next post: