perl error- Can't call method "domain" on an undefined value - perl

#!/usr/bin/perl -w
use Net::SMTP;
$smtp = Net::SMTP->new('mailhost');
print $smtp->domain,"\n";
$smtp->quit;
I run this pl file and get error "Can't call method "domain" on an undefined value"
and in this pl file:
#!/usr/bin/perl -w
use Net::SMTP;
$smtp = Net::SMTP->new('mailhost');
$smtp->mail($ENV{USER});
$smtp->to('postmaster');
$smtp->data();
$smtp->datasend("To: postmaster\n");
$smtp->datasend("\n");
$smtp->datasend("A simple test message\n");
$smtp->dataend();
$smtp->quit;
I get error Can't call method "mail" on an undefined value
What I need todo ?

Has it occured to you that Net::SMTP may have had problems finding your mailhost, and establishing an SMTP connection? I see that you took your scripts directly from the documentation – you do have to supply an actual value for mailhost.
If you had read the documentation a bit further, especially to the documentation for the new method, you'd have found this interesting snippet:
new ( [ HOST ] [, OPTIONS ] )
This is the constructor for a new Net::SMTP object. HOST is the name of the remote host to which an SMTP connection is required.
On failure undef will be returned and $# will contain the reason for the failure.
So let's print out that reson for failure:
my $mailhost = "your mailhost";
my $smpt = Net::SMTP->new($mailhost) or die "Can't connect to $mailhost: $#";
die aborts your program with an error message. This message should tell you more about the actual error.
Do note that the example code in the documentation is not neccessarily meant to be used for real projects – it is just there to showcase the capabilities of the module. For real code, always use strict; use warnings at the top of your code, and declare all your variables with my. This helps finding more errors.

Related

Deciphering File::Fetch error

I was trying to use File::Fetch in a simple script to download a file. Unfortunately, it errors out and I can't seem to find why.
use strict;
use warnings;
use File::Fetch;
my $url = 'https://ftp.mozilla.org/pub/firefox/releases/42.0b7/linux-i686/en-US/firefox-42.0b7.tar.bz2';
my $ff = File::Fetch->new(uri => $url);
my $where = $ff->fetch( to => '/tmp' ) or die $ff->error;
print "Downloaded to ".$where."\n";
At execution:
#./filefetch.pl
Use of uninitialized value in die at ./filefetch.pl line 12.
Died at ./filefetch.pl line 12.
Going into File::Fetch::fetch() with the debugger, the problem is File::Fetch failed to find a way to deal with the https scheme. It appears to have no support for https. It correctly returned false, but it did not populate its error field.
You are correct in assuming it should consider missing a scheme an error. You can report a bug here.

Why am I getting an empty string from calls to IO::Socket::INET->peerhost?

I'm writing a small script to monitor if certain ports are attempted to be accessed on my Linux box (Centos 6) using Perl 5.10.1. I'm getting back blank entries for my peerhost request. I'm not sure why. It sounds like it may be a failure in the IO socket module (http://snowhare.com/utilities/perldoc2tree/example/IO/Socket.html) but I'm not really sure. Any insight would be much appreciated.
EDIT:
Since I enabled the strict and warnings I'm getting an 'uninitialized value $display' in the cases where I thought it was blank.
#! /usr/bin/perl
use strict;
use warnings;
use IO::Socket;
use Sys::Syslog qw( :DEFAULT setlogsock);
use threads;
my #threads=();
my #ports=(88,110,389);
main(\#ports);
sub main
{
my $ports=shift;
setlogsock('unix');
openlog($0,'','user');
for my $port (#{$ports} ) {
push #threads, threads->create(\&create_monitor, $port );
}
$_->join foreach #threads;
closelog;
# wait for all threads to finish
}
sub create_monitor{
my $LocalPort=shift;
my $sock = new IO::Socket::INET (
LocalPort => $LocalPort,
Proto => 'tcp',
Listen => 1,
Reuse => 1,
) or die "Could not create socket: $!\n";
while(1)
{
my $peer_connection= $sock->accept;
my $display = $peer_connection->peerhost();
my $message="Connection attempt on port $LocalPort from $display";
#syslog('info', $message);
print $message."\n";
}
}
NOTE - it is intentional that this script never finish. I'll eventually wrap this with an init script and have it run as a service.
Perl accept() has an error code like most other functions. For accept() it is a false return, see also here.
So when you get undefined as result there is an error in accept() call. The error of accept is saved in the errno variable ($!).
Same is true for peerhost() (see here). It also can fail and return an error code.
If you only use the above code without anything else, then probably you reach connection limit of your system (you should close the connections) when accept() fails. See rlimit() to find out how that number can be increased.
One case where peerhost() fails may be that remote connection was closed already.

Unable to send a mail using perl Mime::Lite

I am unable to send a mail using MIME::Lite. While sending from my desktop it will through the below errors.
Error: "SMTP Failed to connect to mail server: Bad file descriptor"
I am using the below mentioned code.
use strict;
use MIME::Lite;
use Net::SMTP;
my $from_address = "no-reply#host.com";
my $to_address = "madhan#host.com";
my $cc_address = "madhan#host.com";
my $subject = "Test mail";
my $message_body = "Madhan test mail";
my $namer="madhankumar";
my $regards="Madhan M";
print " Sending mail from $from_address to $to_address \n";
my $person_name=ucfirst($namer).",";
my $mail_host = 'mail1.somehost.com';
my $msg = MIME::Lite->new (
From => $from_address,
To => $to_address,
Cc => $cc_address,
Subject => $subject,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";
$msg->attach (
Type => 'TEXT',
Data => "Dear $person_name\n\n".$message_body."\n\nRegards,\n$regards"
) or die "Error adding the text message part: $!\n";
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send;
The above code is working fine while the mail server is connected with LAN. While using the code in remote system the error has been thrown as mentioned in below
"SMTP Failed to connect to mail server: Bad file descriptor".
May I know the reason.. Is the code run in remote system. If not what are the change I have made the code.. Please share your solutions....
Thanks in advance...
Note: I am developing this in Windows XP
The variables do not contain what you think they contain. If you had switched on warnings, you would have noticed this on your own.
$ perl -e'use warnings; my $from_address = "no-reply#host.com";'
Possible unintended interpolation of #host in string at -e line 1.
Name "main::host" used only once: possible typo at -e line 1.
The remedy is to use single quotes to delimit those strings.

Why does my Perl CGI program fail with "Software error: ..."?

When I try to run my Perl CGI program, the returned web page tells me:
Software error: For help, please send mail to the webmaster (root#localhost), giving this error message and the time and date of the error.
Here is my code in one of the file:
#!/usr/bin/perl
use lib "/home/ecoopr/ecoopr.com/CPAN";
use CGI;
use CGI::FormBuilder;
use CGI::Session;
use CGI::Carp (fatalsToBrowser);
use CGI::Session;
use HTML::Template;
use MIME::Base64 ();
use strict;
require "./db_lib.pl";
require "./config.pl";
my $query = CGI->new;
my $url = $query->url();
my $hostname = $query->url(-base => 1);
my $login_url = $hostname . '/login.pl';
my $redir_url = $login_url . '?d=' . $url;
my $domain_name = get_domain_name();
my $helpful_msg = $query->param('m');
my $new_trusted_user_fname = $query->param('u');
my $action = $query->param('a');
$new_trusted_user_fname = MIME::Base64::decode($new_trusted_user_fname);
####### Colin: Added July 12, 2009 #######
my $view = $query->param('view');
my $offset = $query->param('offset');
####### Colin: Added July , 2009 #######
#print $session->header;
#print $new_trusted_user;
my $helpful_msg_txt = qq[];
my $helpful_msg_div = qq[];
if ($helpful_msg)
The "please send mail to the webmaster" message you see is a generic message that the web server gives you when anything goes wrong and nothing handles it. It's not at all interesting in terms of solving the actual problem. Check the error log to find possible relevant error output from your program.
And, go through my How do I troubleshoot my Perl CGI script? advice on finding the problem.
My guess is that you have a syntax error with that dangling if(). What you have posted isn't a valid Perl program.
Good luck,
is that something related to suexec module
Improper configuration of suExec can cause permission errors
The suEXEC feature provides Apache users the ability to run CGI and SSI programs under user IDs different from the user ID of the calling web server. Normally, when a CGI or SSI program executes, it runs as the same user who is running the web server.
apache recommends that you not consider using suEXEC.
http://httpd.apache.org/docs/2.2/suexec.html
From the StackOverflow page: How to trap program crashes with HTTP error code 500
I see that your include: use CGI::Carp (fatalsToBrowser);
... stifles the HTTP 500 error. Simply removing this will allow the programs to crash "properly".

Why can't I connect to my CAS server with Perl's AuthCAS?

I'm attempting to use an existing CAS server to authenticate login for a Perl CGI web script and am using the AuthCAS Perl module (v 1.3.1). I can connect to the CAS server to get the service ticket but when I try to connect to validate the ticket my script returns with the following error from the IO::Socket::SSL module:
500 Can't connect to [CAS Server]:443 (Bad hostname '[CAS Server]')
([CAS Server] substituted for real server name)
Symptoms/Tests:
If I type the generated URL for the authentication into the web browser's location bar it returns just fine with the expected XML snippet. So it is not a bad host name.
If I generate a script without using the AuthCAS module but using the IO::Socket::SSL module directly to query the CAS server for validation on the generated service ticket the Perl script will run fine from the command line but not in the browser.
If I add the AuthCAS module into the script in item 2, the script no longer works on the command line and still doesn't work in the browser.
Here is the bare-bones script that produces the error:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use AuthCAS;
use CGI::Carp qw( fatalsToBrowser );
my $id = $ENV{QUERY_STRING};
my $q = new CGI;
my $target = "http://localhost/cgi-bin/testCAS.cgi";
my $cas = new AuthCAS(casUrl => 'https://cas_server/cas');
if ($id eq ""){
my $login_url = $cas->getServerLoginURL($target);
printf "Location: $login_url\n\n";
exit 0;
} else {
print $q->header();
print "CAS TEST<br>\n";
## When coming back from the CAS server a ticket is provided in the QUERY_STRING
print "QUERY_STRING = " . $id . "</br>\n";
## $ST should contain the received Service Ticket
my $ST = $q->param('ticket');
my $user = $cas->validateST($target, $ST); #### This is what fails
printf "Error: %s\n", &AuthCAS::get_errors() unless (defined $user);
}
Any ideas on where the conflict might be?
The error is coming from the line directly above the snippet Cebjyre quoted namely
$ssl_socket = new IO::Socket::SSL(%ssl_options);
namely the socket creation. All of the input parameters are correct. I had edited the module to put in debug statements and print out all the parameters just before that call and they are all fine. Looks like I'm going to have to dive deeper into the IO::Socket::SSL module.
As usually happens when I post questions like this, I found the problem. It turns out the Crypt::SSLeay module was not installed or at least not up to date. Of course the error messages didn't give me any clues. Updating it and all the problems go away and things are working fine now.
Well, from the module source it looks like that IO::Socket error is coming from get_https2
[...]
unless ($ssl_socket) {
$errors = sprintf "error %s unable to connect https://%s:%s/\n",&IO::Socket::SSL::errstr,$host,$port;
return undef;
}
[...]
which is called by callCAS, which is called by validateST.
One option is to temporarily edit the module file to put some debug statements in if you can, but if I had to guess, I'd say the casUrl you are supplying isn't matching up to the _parse_url regex properly - maybe you have three slashes after the https?