Scrapdiary

DesigningとEngineeringの架け橋

PHPのphpinfo();のように情報を出力したい

サーバにshellで潜れず、(S)FTPしか許可されていないような場合に情報を出力することが出来ればと。そのサーバがどんな仕様か不明なのでモジュール関連は一切何も使わないというのがコンセプト。

perlinfo.cgi

#!/usr/bin/perl

use strict;
use warnings;

my $title = "Information about $ENV{'HTTP_HOST'}";
my %mod_list;
my $version = `perl -v`;
$version =~ s/\n\n/\<br\>\n/g;

print  "Content-type: text/html\n\n",
       "<html><meta http-equiv=Content-Type content=text/html; charset=iso-8859-1>",
       "<title>$title</title><body>\n",
       "<h1>$title</h1><hr>\n",
       "<h2>Version (perl -v):</h2>\n",
       "<p>$version</p><hr>\n",
       "<h2>Environment values:</h2>\n",
       "<ul>";

foreach my $key( keys %ENV ){
    print "<li>$key: $ENV{$key}</li>\n";
}

print  "</ul><hr>\n",
       "<h2>Include path:</h2>\n",
       "<p>";
print  "$_\n" for @INC;
print  "</p>\n",

print  "</ul><hr>\n",
       "<h2>Installed module list:</h2>\n",
       "<ul>";

&listup($_) for grep {$_ ne '.'} @INC;
print "<li>$_</li>\n" for sort keys %mod_list;

my($user, $system, $cuser, $csystem) = times;

print  "</ul><hr>\n",
       "<p>User CPU times:$user / ",
       "System CPU times:$system / ",
       "Child process user CPU times:$cuser / ",
       "Child process system CPU times:$csystem (sec)</p>\n",
       "</body></html>\n"; 
exit;

sub listup {
       my ($base, $path) = @_;
       (my $mod = $path) =~ s!/!::!g;
       opendir DIR, "$base/$path" or return;
       my @node = grep {!/^\.\.?$/} readdir DIR;
       closedir DIR;
       foreach (@node) {
              if (/(.+)\.pm$/) { $mod_list{"$mod$1"} = 1 }
              elsif (-d "$base/$path$_") { listup($base, "$path$_/") }
       }
}

そのスクリプトが実行された処理時間をとるのはtimes関数でいいのかな。「listup」関数はどこからからコピペしたので、後でしっかり理解したいところ。
実は車輪の再発明かも。でも勉強と割り切ってますのであしからず・・・と思って調べたらたくさんありますね。モジュール使ってますけど。
こちらにまとまってました。

だよねー。みんな考えることは一緒ってことだ。

※追記:@INCのPathを追加