Respected ppl ...
Im on openSUSE 12.2 and my Apache server is running fine ...
I copied the index.html which contains :
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>Program 7b</title></head>
<body>
<form method="POST" action="http://localhost/cgi-bin/7b.pl">
Please Enter the Command :
<input type="text" name="command" id="command" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
And the perl file as follows :
#!/usr/bin/perl
print "Content-type:text/html\n\n";
use CGI
$a = new CGI;
$comm = $a->param("command");
print "The Output of the entered command is:<br />";
system($comm);
Both files in the cgi-bin directory
But i get the error :
Premature end of script headers
On running localhost/cgi-bin/index.html in any browser ....
I have set the execution of perl files in the apache configuration ...
Kindly help with the problem ...
Regards
-SkyKOG
Apache is running index.html as a script, not simply displaying an HTML document. Apache is probably configured to run anything in the cgi directory as a script.
Move the html file into another directory.
Try the following working code :
#!/usr/bin/perl
use strict; use warnings;
use CGI;
print "Content-type:text/html\n\n";
my $a = new CGI;
my $comm = $a->param("command");
print "The Output of the entered command is:<br />";
system($comm);
An advice : never forget use strict; use warnings; and declare variables with my or our.
Related
Newbie question: I have a simple script that builds a text file with the sendmail headers, required blank line and then text body. When I run:
sendmail -t < email.txt
it fails with a "No recipient addresses found in header" error.
However, when I run:
cat email.txt | sendmail -t
it works and the recipients receive my email.
I'm just wondering why it is behaving this way.
I'm on an AIX 6100-09-12-1846 (oslevel -s) box.
I just tested on a Linux box and it worked both ways, so I can only assume that AIX handles it differently, but I don't know why.
Update:
The following contents are in "email.txt"
Subject: Test Email
To: foo#bar.com
From: Mail.Relay#bar.com
MIME-Version: 1.0
Content-Type: text/html
<HTML>
<HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
</HEAD>
<BODY lang=EN-US link=blue vlink=purple>
<FONT SIZE=8>
This is a test email.<BR/>
</FONT>
</BODY>
</HTML>
But only when using "|" into sendmail does it work, not when redirected using "<". I'm confused.
Any ideas?
Thanks,
racer
I am trying to get the android:versionName from manifest.xml file and set the value as environment variable using Perl script . as i am new to Perl can anyone help on this.
Here is the my manifest file.
I want to get android:versionName value and set the same value as environment variable using perl script. can you please anyone help on this.
manifestfile.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="schemas.android.com/apk/res/android"; package="com.dev.mobile" android:versionCode="75" android:versionName="1.0.1.68">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />'
I'd suggest for XML, the answer is almost always 'use a parser'.
XML::Twig is quite lightweight and simple. (XML::LibXML is more fully featured, and is a good alternative).
And to do your thing:
#!/perl/bin/perl
use warnings;
use strict;
use XML::Twig;
my $xml = XML::Twig -> parsefile ( 'manifestfile.xml');
print $xml -> root -> att('android:versionName'),"\n";
Looking at your use case the below code should work:
#!/usr/bin/perl -w
open FP, "manifest.xml";
$version="";
while (<FP>)
{
if (( /android:versionName/) && ( /versionName=(.*)\>/ ))
{
$version=$1;
last;
}
}
print $version;
The avoiding of the "HTML Generation functions" doesnt work for me.
If I start on my localhost the perlscript
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print '<html>';
print '<head>';
print '<title>Hello Perl</title>';
print '</head>';
print '<body>';
print '<h2>Hello perl</h2>';
print '</body>';
print '</html>';
My browser print it out as expected:
"Hello perl"
But perldoc advices: "HTML Generation functions should no longer be used". So I tried the object oriented cgitutorial-script (perl, v5.10.0).
#!/usr/bin/perl -w
use CGI;
$q = new CGI;
print $q->header,
$q->start_html('hello world'),
$q->h1('hello world'),
$q->end_html;
The browser shows
"Internal Server Error"
If I load the above script on the terminal with
$perl mycgi.pl
it shows an expected outcome
Content-Type: text/html; charset=ISO-8859-1
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>hello world</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>hello world</h1>
</body>
</html>
What am I missing?
From your comment:
(8)Exec format error: exec of '/Library/WebServer/perling/mycgi.pl' failed
That usually indicates a problem with your shebang line. Is the shebang on the first line of your program? Does it point to an executable that really exists? Are there spurious invisible characters at the end of the line (this is sometimes the case when a program is written on Windows and run on a Unix system).
I am trying to control GPIO pins over the web. I have apache server installed on my raspberry-pi(Raspbian wheezy) and I have created a web page with Html and javascript. What I want to do is to control the gpio pins when a user clicks a button on a page.
I have bash script on a .cgi file in /usr/lib/cgi-bin directory and I made certain arrangements on apache configuration file so that it can access the file on that location.
Here is the .cgi file content:
#!/bin/bash
gpio -g mode 7 out
gpio -g write 7 1
echo "Status: 204 No Content"
echo "Content-type: text/html"
echo ""
changes to the conf file of apache: /etc/apache2/sites-enabled/000-default
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
AddHandler cgi-script .cgi // I added this line
</Directory>
but when I click button on the page it shows no response. I have also made the .cgi file executable with this command:
sudo chmod a+x setu.cgi
page code:
head section:
<head>
<script language="JavaScript" type="text/JavaScript">
function setu()
{
document.location="cgi-bin/setu.cgi";
}
function clearall(event)
{
document.location="cgi-bin/clearall.cgi";
}
</script>
</head>
body section:
<form name="form1" method="post" action="">
<p align="center"> </p>
<p align="center">
<input name="up" type="button" id="up" value="UP" onmousedown="setu()" onmouseup="clearall(event)" >
<p align="center"> </p>
</form>
Any help on this topic would be great.
Enable mod_cgi module for apache2
sudo a2enmod mod_cgi
Depending on what architecture your processor is it will enable cgi or cgid module
Worked for me.
I'm using the following code to test the outputting of fatal errors to the browser:
use CGI;
use CGI::Carp qw(fatalsToBrowser);
die "test";
I am expecting to see some error in the browser, but there isn't any, I just get a regular 500 response. I forgot that I had custom error pages on for remote requests, and I now get Script failed to send data..
Also:
> perl -w index.pl
Status: 500
Content-type: text/html
<h1>Software error:</h1>
<pre>test at index.pl line 4.</pre>
<p>
For help, please send mail to this site's webmaster, giving this error message and the time and date of the error.
</p>
[Mon Feb 8 18:29:52 2010] index.pl: test at index.pl line 4.
Try printing a couple of new lines before anything. That signals the end of HTTP headers to the server as the 'CGI standard' says. Or you may be able to use something like this: (as copied from the Carp man page):
use CGI::Carp qw(set_die_handler);
BEGIN {
sub handle_errors {
my $msg = shift;
print "content-type: text/html\n\n";
print "<h1>Oh gosh</h1>";
print "<p>Got an error: $msg</p>";
}
set_die_handler(\&handle_errors);
}
If that doesn't work here are a few more tricks:
#!perl
BEGIN { $| = 1; print "\n\n" }
open STDERR, ">&STDOUT";
..And a few more tricks in this Perl Journal article.