I have the following test script to send an email:
use strict;
use Net::SMTP;
print "Content-Type: text/plain\n\n";
print "Sending email...\n";
my $smtp = Net::SMTP->new('10.0.0.1', Port => 25, Timeout => 10, Debug => 1);
$smtp->mail("user1\#domain.local");
$smtp->to("user2\#domain.local");
$smtp->data();
$smtp->datasend("From: user1\#domain.local\n");
$smtp->datasend("To: user2\#domain.local\n");
$smtp->datasend("Subject: Test\n\n");
$smtp->datasend("Testing 1 2 3\n");
$smtp->datasend();
$smtp->quit;
It works fine when I run it from the command line, I get the email right away. But when I put it in C:\inetpub\wwwroot and run it from a web browser, I get the Sending email... text but then nothing. No email is sent, no error message is shown. I looked at the mail server log and no connection is even made. I'm not sure why it's working from cmd but not from IIS. Is there some extra configuration needed for the script to do this through IIS?
I also tried with sendmail() and get similar results.
First of all, add the following headers to the file
use strict 'vars';
use warnings;
use diagnostics;
use feature qw/say/;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
... your code...
# Print Warnings
warningsToBrowser(1);
That will give you more information if it's failing or throwing a warning on something.
Secondly, how did you install Net::SMTP? Make sure it, and all it's dependancies have permissions by the IIS worker process.
Related
Hello
I have got a LAMPP Webserver. I tried to open a CGI Script called "Hello.cgi".
It contains:
#!/usr/bin/perl
print "Hello World.\n";
The path is: /opt/lampp/htdocs/dashboard/cgi-bin/hello.cgi
When I open: "127.0.0.1/dashboard/cgi-bin/hello.cgi", I get following:
Server error!
The server encountered an internal error and was unable to complete your request.
Error message:
End of script output before headers: hello.cgi
If you think this is a server error, please contact the webmaster.
The CGI Script is written in Perl.
If you need more informations about my problem, say it, please.
~~runasas
Firstly, you should never write a Perl program without including the lines use strict; and use warnings;.
Secondly, if you have problems with a CGI program, you should check the web server error log for more details of the problem.
Thirdly, the output from a CGI program needs to include a content type header. So you'll want to add the following:
print "Content-Type: text/plain\015\012\015\012";
This becomes easier if you use the CGI module:
use CGI;
print header('text/plain');
But really, in 2016, you shouldn't be writing CGI programs - there are plenty of good alternatives available.
I am trying to learn Perl so I need to be able to test it locally. I have IIS7.5 and I have the pl and cgi mapped. But I am throwing this error when I test it in Chrome.
It works fine when I use the command prompt. So I don't understand what I am missing or how to troubleshoot this. I've searched all over google, can someone help me.
My test code:
#!/usr/bin/perl
use warnings;
use strict;
use CGI;
my $test = new CGI();
print $test->header("text/html"),$test->start_html("PERL Test");
print $test->h1("PERL IS WORKING!");
print $test->end_html;
And the command console spits out a perfectly programmed HTML page:
I have a perl cgi script that is exactly the following:
#!/usr/bin/perl -w
use CGI;
$query = new CGI;
print $query->redirect("http://www.yahoo.com");
At the command line things look OK:
$perl test.pl
Status: 302 Moved
Location: http://www.yahoo.com
When I load it in the browser, http://localhost/cgi-bin/test.pl, the request gets aborted, and depending on the browser I get various messages:
Connection reset by server.
No data received.
The only research I could find on this issue, stated that a common problem is printing some data or header before the redirect call, but I am clearly not doing that here.
I'm hosting it from a QNX box with the default slinger server.
The code works fine on my machine, check the following
Check the error logs, eg: tail /var/log/http/error_log
Do the chmod/chown permissions match other working CGi scripts, compare using ls -l
Does printing the standard hello world work? Change your print statement to
print $query->header(), 'Hello World';
Add the following for better errors
use warnings;
use diagnostics;
use CGI::Carp 'fatalsToBrowser';
at the command line use slinger will return some basic use options. For logging you need both syslogd and -d enabled in slinger. Ie
slinger -d &
Then look to /var/log/syslog for errors
I am rather new in Perl. I have written a very simple script that copies and removes a file from sftp.
but the script should return some kind of output in order to integrate it with nagios.. using nsca or something.
the script is running on solaris 10.
here is the script:
#!/usr/bin/perl -w
use strict;
use warnings;
use Net::SFTP::Foreign;
use POSIX qw(strftime);
my $datestring =strftime "%d-%m-%Y", localtime;
my $host="sftp.mariog.com";
my $username="user";
my $local="/mnt/mariog";
my $file="BookingReport_Daily_$datestring.xls";
print "$file \n";
my $sftp = Net::SFTP::Foreign->new($host,
user => $username,
stderr_discard => 1,
autodie => 1,
);
$sftp->die_on_error("unable to establish SFTP Connection");
$sftp->get("$file", "$local/$file");
$sftp->remove($file);
$sftp->disconnect();
how to handle the fact that the file does not exist on the sftp? maybe it has not reached yet. there a daily file uploaded but at different times. the script is run by cron every 4 hours so most of the times it will not find files to transfer...
where can I get the output code of the transfer successful or not? so that i can pass it to a nagios passive check with nsca...
thank you for your help..
kind regards.
Mario
It looks like one way to check for a successful get in the documentation would be something like this:
$sftp->get("$file", "$local/$file") or die "get failed:" $sftp->error;
I'm executing remote commands using Net::OpenSSH using a web frontend. My commands return without failure on the command line, but I get nothing in a web browser. I've done a couple hour research to no avail--any ideas?
Here is some code to give you an example (some removed for obvious reasons).
#!/usr/bin/perl -w
use strict;
use CGI ':standard';
use Net::OpenSSH;
# Here in the code is just the header and standard tags
print "1";
print "2"; # both display
my $ssh = Net::OpenSSH->new($host, user => $uname, key_path => $key); # all works
$ssh- error and die "Can't ssh to host" . $ssh->error;
print "3";
$ssh->system("uname -a") or
die "remote command failed: " . $ssh->error;
my #lsa = $ssh->capture("ls -a");
$ssh->error and
die "remote ls command failed: ". $ssh->error;
print "4";
print "5";
print #lsa; # won't display in browser, just terminal/CLI
Cheers!
I maintain CGI.pm. I recommend these additions to your simple script:
Before you print anything else, print the standard HTTP header: print header();
Add this after the use CGI line: use CGI::Carp qw(fatalsToBrowser); ... that will display any run-time problems in the browser. If you don't get any output after these changes, check that the script compiles with perl -cw script.pl
Below is about the minimum Perl code that worked for me on Debian machine. I suggest you go through it and compare it to your actual code.
However, it did not work out-of-the box on my Debian, I had make some decisions most of which probably aren't very safe, but that's more about specific environment:
make home for user that server runs writable (/var/www)
add host to ~/.ssh/known_hosts beforehand
use the strict_mode => 0 to bypass Net::OpenSSH's security checks instead of finding proper
ctl_dir (Net::OpenSSH requires that the folder and all above folders are 0755 or more strict,
so /tmp I used is normally not good enough)
I believe there are much safer practices than that, but as I said, that's specific to environment.
So the code:
#!/usr/bin/perl
use strict;
use warnings;
use Net::OpenSSH;
use File::Temp qw/ tempdir /;
# necessary minimum for CGI
print "Content-type: text/plain\n\n";
# prepare temp dir
my $temp = tempdir("/tmp/sshme.pl-XXXXXXXX", CLEANUP => 1);
# open SSH session
my %opts = (
user => "user",
password => "password",
ctl_dir => $temp,
strict_mode => 0 ## NOT recommended - see my comments
);
my $ssh = Net::OpenSSH->new("host", %opts);
$ssh->error
and die "Couldn't establish SSH connection: ". $ssh->error;
# perform command and print output
my #lines = $ssh->capture("ls")
or die "remote command failed: " . $ssh->error;
print #lines;
Perhaps your errors get directed to standard error, not standard output. In that case, they'll usually end up in the server log, not the browser window. Perhaps you can use POSIX::dup2 to avoid this:
use POSIX;
# Make sure to send HTTP headers before redirecting streams!
POSIX::close(2); # close original stderr stream. No more output to apache logs!!!
POSIX::dup2(1,2); # redirect error stream to standard output, so errors will appear in browser.
Processes launched by perl, like e.g. some ssh binary, will inherit these streams.