The #exec CommandThere are occasions when SSI includes and SSI variables cannot achieve the dynamic content you want, but you still don't want to go to the trouble of writing a CGI program to generate the entire page content. By using the #exec command, you can include the output of a program within your HTML pages. The #exec command is followed by the cgi parameter, and a valid CGI program as the attribute. <!--#exec cgi="prog.cgi" --> Simple Example: A CGI to Display Machine LoadTo prove that your server machine is capable, you might want to include a load meter on the machine spec page. The page contains a lot of information and is updated frequently so you don't want a CGI program to generate the complete HTML code. The examples below will obtain the load average of the machine, then determine what colour to display the text. The .shtml file: <html> <head><title>SSI Test</title> <meta http-equiv="Refresh" content="2; test.shtml"> </head> <body> This is a big page about our large machine. <p> <!--#exec cgi="load.cgi"--> </body> </html> The CGI Program:
#!/usr/bin/perl
use strict;
print( STDOUT "Content-type: text/html", "\n\n" );
my $uptime = `uptime`;
my @fields = split( /,/, $uptime );
my $load = $fields[ -2 ];
my $colour = undef();
if ( $load < 1 ) { $colour = "white" }
elsif( $load < 2 ) { $colour = "yellow" }
elsif( $load < 3 ) { $colour = "orange" }
else { $colour = "red" }
print( STDOUT <<TXT );
<p>Load average is <font color="$colour"> $load </font></p>
<hr>
TXT
END
Possible (this is dynamic after all) result in the HTML: <html> <head><title>SSI Test</title> <meta http-equiv="Refresh" content="2; test.shtml"> </head> <body> <p>This is a big page about our large machine. </p> <p>Load average is <font color="white"> 0.32 </font></p> <hr> </body> </html> Executing arbitrary shell commandsThe #exec command can also be used to execute arbitrary shell commands when used with the cmd = tag. For this tag to be available, the server needs to have `CGI enable anywhere' turned on. <!--#exec cmd="command" --> The server will execute the equivalent of /bin/sh -c "command" and replace the tag with the output of that command. For example: <!--#exec cmd="cat /etc/passwd | cut -f1 -d':' | sort" -->
Content Manager
[Administrator] 16 December 2005
|
Recent Articles
Other Resources
|


