Providing practical examples since 1998

Things look quiet here. But I've been doing a lot of blogging at
dan.langille.org because I prefer WordPress now.
Not all my posts there are FreeBSD related.
I am in the midst of migrating The FreeBSD Diary over to WordPress
(and you can read about that here).
Once the migration is completed, I'll move the FreeBSD posts into the
new FreeBSD Diary website.

This article is about my first CGI script. That may not sound like much, but I
found it was pretty exciting. Everything I'd written today on my websites had been
static. This script shows you some dynamic content.

If you've never done CGI
scripts before, you're going to be amazed at how simple it is.

CGI means Common Gateway Interface. CGI is the standard for running external
programs from an http server. With a CGI script, you can access information in a
database, or run a perl script (which is what I did). It is the most common method
used to provide dynamic information on a webpage (most of which are static).

Most web
pages are static. What you type is what you see. Every time you see it.
But with a CGI script, you can put anything you want in your HTML script. I used uptime
as my example.

uptime

uptime is the utility which displays the length of time the system has been
up (as if you didn't know). Here's an example output:

I did encounter one problem when setting this up. If you wind up
seeing the uptime script in your browsers, then you have a problem with your virtual
hosts. You may want to see Perl scripts that appear in
your browser.

You
don't have to use perl to do this. In fact, you can just use a plain old shell
script. It turns out to be much faster. I don't have mod_perl installed with
my apache, but I'm told that will make perl as fast as the shell script..

But be warned. Something simple like this script is pretty low risk, from a
security point of view. But be careful with any script you write.

#!/bin/sh -
echo "Content-Type: text/plain"
echo ""
/usr/bin/uptime

Not only is this script smaller, it executes faster. As always, comments are welcome.

The reason the shell is much faster is two-fold: you're not firing off a Perl instance
to interpret and run your code, and -- more importantly -- you're not including 200K of
code (6481 lines or so) that follows CGI.pm around.