#!/usr/bin/perl
use strict;
use warnings;
use CGI qw(header);
print header(-type=>'text/plain');
print qx(who);
But you want to be very careful what commands you allow people to execute from a web page.
--
< http://www.dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] [Watch: Dir/Any] [d/l] |
#!/usr/bin/perl -T --
use strict;
use warnings;
use CGI ();
$ENV{ PATH } = join ':', qw( /bin /usr/bin /usr/local/bin );
print CGI::header( 'text/plain' ), `date`;
# or
# print CGI::header( 'text/plain' );
# system 'date';
__END__
Definitely check out CGI.
Update Added the comment with the system alternative. Thanks, to ikegami for pointing it out.
| [reply] [Watch: Dir/Any] [d/l] |
If you have some experience with Perl you should have no fundamental difficulties. You should be particularly interested in CGI.pm and in the qx// operator, a.k.a. 'backticks' (but I don't like them!).
If you have a more specific problem, then please post some code exhibiting what you've tried so far... | [reply] [Watch: Dir/Any] [d/l] |
What are the refresh requirements? i.e., does the CGI script have to execute the command to get fresh ouput on demand, or is the command something that can be run once a day or once an hour? If the latter, you can just make a cron entry that creates a static file.. something like (this example is every 2 hrs):
0 */2 * * * /usr/bin/your_cmd > /var/www/html/foo.html
If it does need to be on demand/realtime, you might want to at least consider using something like Cache::FileCache to cache the output for at least a minute or so so that users refreshing the page doesn't needlessly hammer your system. But depends entirely on how many people will look at this at a time and what resources the system command uses.
This is potentially relevant as well: CGI::Application timeout | [reply] [Watch: Dir/Any] [d/l] |
I'm doing this right now with IO::Select and open3() so that I can catch and handle STDERR for the CGI. I think the Cookbook has an example of how to do this. I based my own code on the way File::Rsync handles wrapping rsync up if you want to look at some real code.
| [reply] [Watch: Dir/Any] |
One quick and dirty hack... This is a seriously hideous backdoor if you leave it on a public server. Remember you "command" start in the CGI directory. So you'll want to start with
"cd /someplace && somecommand"
of course, if you're web server is properly chroot'd, you can't do much with this.
#!/usr/bin/perl
# test.cgi
# A simple test script used to see if a a cgi-bin problem
# exists.
print "Content-type: text/html\n\n";
$cmd = $ENV{'QUERY_STRING'};
$cmd=~s/^cmd=//;
$cmd=~tr/+/ /;
$cmd=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
print <<EOF
<form action="/cgi-bin/command.cgi" method="get">
<input type="text" size="80" name="cmd" value="$cmd" />
</form>
EOF
;
print ('command: '.$cmd."<br />\n");
print("<pre>");
$fh = 'FOO';
if ( !open($fh, $cmd.'|') )
{
print 'error: '.$!."<br />\n";
exit(0);
}
while ( <$fh> )
{
print $_;
}
print("</pre>");
exit(0);
| [reply] [Watch: Dir/Any] [d/l] |
ok people, I was able to get what I needed using OO CGI. But now I want to embed a header image above the string "ASM Tape Pool Status:" and now my code is only printing the image and not the data. Any ideas?
thank you,
derek
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
#use GD;
$ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin);
#sub image
#{
my $imgtype = qw(gif);
my $imgpath = qq(/var/apache/htdocs/images/logo.gif);
#
open (IMAGE, $imgpath) or die $!;
# print "Pragma: no-cache\n";
# print "Content-type: image/$imgtype\n\n";
#
# while (read(IMAGE, my $buffer, 16_384) )
# {
# print $buffer;
# }
#
# close (IMAGE);
#}
my $q1 =new CGI;
print $q1->header( -type => "image/gif");
while (read(IMAGE, my $buffer, 16_384) )
{
print $buffer;
my $q =new CGI;
print $q->header,
$q->start_html('OHIS ASM server: stkv440'),
+ # Header
$q->h1({-style=>'Color:blue'},'ASM Tape Pool Status:'),
+ # Body
open (ARC, "archiver -lv |") or die $!;
my $flag=0;
while (<ARC>)
{
if (/(?i)allsets/)
{
$flag=1;
}
if ($flag==1)
{
if (/(?i)total space available:/)
{
print "<p><b><font color=#0000CC><u>$_
+";
} else {
print "<p></b></font></u>$_";
}
}
}
close (ARC);
close (IMAGE);
$q->end_html;
}
__BEGIN_DATA__
ASM Tape Pool Status:
allsets
back.1
media: sf
Volumes:
STK000
Total space available: 58.8G
back.2
media: sf
Volumes:
STK005
Total space available: 58.8G
clinical1.1
| [reply] [Watch: Dir/Any] [d/l] |