My first CGI script (uptime.pl)
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.
Since first writing this article, I’ve been told how you can use a shell script instead of a perl script.
CGI
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:$ uptime 1:53PM up 99 days, 5:49, 1 user, load averages: 1.28, 1.11, 1.09The load averages shown are high because I’m running RC5/DES. Otherwise they’d be much closer to zero.
The script
Here’s the uptime.pl script which is located in my cgi-bin directory under the root directory of my webpage (see below for a faster perl version)#!/usr/bin/perl use CGI; $query = new CGI; print $query->header('text/html'); print `uptime`
The HTML
Here is the HTML which uses the above script:<p>click here for our <a href="cgi-bin/uptime.pl">uptime</a></p>
Problems
But you don’t have to use perl!
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.
A faster perl version
Kanji T Bates bates@jurai.net wrote in with this faster version of my original script. Kanji wrote: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.
Kanji also supplied this lean and mean perl script:
#!/usr/bin/perl print "Content-Type: text/plain\n\n", `uptime`;
Here are some benchmarks from Kanji’s P100:
Script
Time
uptime.sh 0.040s uptime.pl 0.082s uptime.cgi 1.031s
A working example
Here’s the webpage which puts all of the above together:And a php example using passthru
Marc Silver wrote in with this example using php3:<?php $uptime = passthru ("/usr/bin/uptime"); echo "<font face=\"Tahoma\" SIZE=\"1\">$uptime</font></p>"; ?>
the PHP function passthru() outputs the raw result from ps aux and returns no value. so in the above code segment, printing that variable would serve no purpose.
Right you are. Thanks.
For the record, here’s the original script:
<title>FreeBSD Diary uptime via php3 passthru</title>
<body>
<?php
$uptime = passthru ("/usr/bin/uptime");
echo "<font face=\"Tahoma\" size=\"1\">$uptime</font></p>";
?>
</body>
</PRE>
The new script is:
html><title>FreeBSD Diary uptime via php3 passthru</title>
<body>
<font face="Tahoma" size="1"><?php
passthru ("/usr/bin/uptime");
?></font></p>
</body>
But I’m going to remove the font declaration anyways.
I’ll amend the article accordingly.
i did notice, that this doesnt work if you have safe mode on, any ideas as to how to get it working if safe mode IS on?
hi
iam facing a programming problem right now and i urgently need to find a solution to it:
I have written a shell script that:
1. Retrieves system information from SunOs and HP-UX servers
2. Formats the data into a readable and interpretable form
Now the last step is to write these data into an SQL SERVER 2000
database.
i find myself at a dead end here.
i really can’t move on coz i don’t know how to do it.
Can u please help me over here.
Thanks- plz reply to the given email address(above)
Ranjana