Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create webpage from shell command - cgi script

Status
Not open for further replies.

adajun

MIS
Jan 18, 2005
2
0
0
US
Hi I need to create my first cgi script to list the services running on a windows server from my nagios box. The windows box has openssh on it.

I have a bash shell, but that doesn't seem to work, so I want to try to do a perl script. Here's my 3 line shell script with 2 lines of comments.

#!/bin/sh
# doit.sh aej 2008-nov-13
ssh winserver01 "sc query" | /usr/local/bin/txt2html

when I run the shell script from command line as the apache user, I see html scroll by - I can pipe it to a file and open that file in firefox or IE. When I enter the path to the doit.sh script in my browser, I get nothing on the page and then in the apache error log, I get.
"malformed header from script. Bad header=<!DOCTYPE html PUBLIC "-//W3C/: doit.sh"

any ideas?

thanks

adam
 
Oh yeah, I also tried this little trick to call a shell command from perl and got the same results.

#!/usr/bin/perl
$cmd = "/usr/local/nagios/cgi-bin/doit.sh;
system $cmd;
 
Maybe...

Code:
#!/usr/bin/perl -w

# print http headers
print "Content-Type: text/html\n\n";

# make results preformatted
print "<pre>";

# do your system command here
my $output = `ssh winserver01 "sc query" | /usr/local/bin/txt2html`;

# print output
print $output;

# end pre tag
print "</pre>";

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
er, actually you can prolly ignore the bit about the pre tags if txt2html handles the formatting for you.

But the idea is, all CGI scripts must print a set of HTTP headers (usually a Content-Type header) and then a blank line and then the rest of the page content. So the Content-Type line there should satisfy the Apache errors.

You could make your sh script be the CGI if you don't like Perl for any reason. Just echo "Content-Type: text/html" and then echo another blank line and then do whatever else.

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top