Monday, March 3, 2008

Using Bash To Report On Web Server Usage - Porting From Perl

Hey there,

Today's post is a follow up to the CGI script we looked at yesterday in our post on reporting on web server log elements. I went on a bit about the security implications with regard to both Linux and Unix, and I apologize if it was annoying at all ;) I feel very strongly about the subject, since I'd hate to see anyone out there get compromised for no good reason almost as much as I'd hate to have that happen to me!

The script we're presenting today is a more secure rewrite of that script; although, technically not a "rewrite" in the traditional sense. We haven't honed the code or made it more browser-friendly. Instead, with an eye towards porting, it's been completely rewritten in bash. It still does the same thing (counts jpg and gif hits as compared to HTML) It is also written to be run from the command line (using brute force scripting again :), rather than from a browser, which can be done very simply by running something like this (after you've named it whatever you want and set the execute permissions properly):

host # ./htmlElementCount.sh

As we've noted in the past, as soon as we get some time to quit furiously hammering out (possibly) useful scripts, we'll be presenting a series of posts on porting between Perl and shell. Until that's complete, it would be slightly less than truthful to promise a week's worth of fleshing out the basics of porting between languages. About half of it's done already, but we want to finish it all up before we put it out there for your amusement and/or use ;)

Cheers,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/bash

#
# htmlElementcount.sh
# Count what people are accessing on your site
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

access="/wherever/your/http/logs/are/access_log"
count=0
html=0
gif=0
jpg=0

while read line
do
let count=$count+1
echo $line | grep -iq ".html"
if [ $? -eq 0 ]
then
let html=$html+1
continue
fi
echo $line | grep -iq ".gif"
if [ $? -eq 0 ]
then
let gif=$gif+1
continue
fi
echo $line | grep -iq ".jpg"
if [ $? -eq 0 ]
then
let jpg=$jpg+1
continue
fi
done <$access

echo "Page hits"
echo
echo "$count pages accesssed - Form Elements Processed:"
echo "$html html pages accessed."
echo "$gif GIF files requested."
echo "$jpg JPEG files requested"



, Mike