Showing posts with label gif. Show all posts
Showing posts with label gif. Show all posts

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




Sunday, March 2, 2008

Web Server Log Element Reporting Using CGI

Hey there,

Today's post is another quick and simple CGI script, written in Perl for Unix or Linux. It should be noted at the outset that, even though I like to do this sort of thing for my own "ease of use," it's nothing I actually endorse doing in any sort of an environment where security is a factor.

The script we're presenting today is fairly simple and also fairly simple to compromise. It basically goes against one of the main tenets of our post on processing emails with CGI. For security's sake, you should "never" allow direct access to any file, or initiate any process, directly through a simple CGI page. This opens you up to a thousand different ways a user can insert form values and, possibly, read your local files and/or run processes you didn't want them to on your local machine!

In any event, for experience's sake, just create this as a .pl or .cgi file and load it up in your web browser (be sure to set execute permissions on it - 755 should cover you) and enjoy your page element counts. You can change the main variables as much as you like, but this script is just looking at how many times our user base looked at pictures (jpg's and gif's) as compared to HTML pages. The possibilities are only limited to your imagination. And your willingness to take this risk ;)

Tomorrow, we'll look at doing this without CGI, using the bash shell instead of Perl. Yes, even more porting examples leading up to the series of concept articles :)

Enjoy,


Creative Commons License


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

#!/usr/bin/perl

#
# htmlElementcount.pl
# 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
#

print "Content-type: text/html\n\n";
$access = "/wherever/your/http/logs/are/access_log";

$count=0;
$html=0;
$gif=0;
$jpg=0;

open(ACCESS, $access) or die "Can't open access log!\n";

while(<ACCESS>) {
$count++;
$html++ if /\.html/;
$gif++ if /\.gif/;
$jpg++ if /\.jpg/;
}

print "<html>\n";
print "<head><title>Page hits</title></head>\n";
print "<body>\n";
print "$count pages accesssed - Form Elements Processed:\n";
print "$html html pages accessed.\n";
print "$gif GIF files requested.\n";
print "$jpg JPEG files requested\n";
print "</body>\n";
print "</html>\n";
close(ACCESS);
exit;



, Mike