Solved

How do i know the perl 'Can't call method "login" on an undefined value at' error cause

Posted on 2011-02-16
12
955 Views
Last Modified: 2012-05-11
Hi,

I have problem with my perl script.

This perl script i added on my crontab that will run every hour.
But sometimes when i checked the log, the ftp is not running and got message :

Can't call method "login" on an undefined value at

But the thing is, this is intermeatten. Let's say at time 01.00, 02.00 it works then at 03.00, it is not working
then 04.00 it's working again, 05.0 it's not working again.

I was wondering what is the cause of it ?
I understand that this is because the perl is unable to call Net::Ftp module.
But what i don't understand is what is the cause of the ftp can not call the module or login ?

I try to add some ping command, but unfortunately i don't have any root password, so i can not use ping command.


use Net::FTP;

&log ("Trying to get new FTP connection");
$ftp=Net::FTP->new($host,Debug => 1) or $newerr=1;
  push (@ERRORS, "Can't ftp to $host: $!\n") if $newerr;
  &log ("Error: $newerr ", @ERRORS) if $newerr;
&log ("Connected");

&log("Login FTP server, host = $host, user = $user, password not shown");
$ftp->login($user,$passwd) or $newerr=1;


0
Comment
Question by:budihartono
12 Comments
 
LVL 14

Expert Comment

by:DonConsolio
ID: 34913198
Net::FTP->new returns a Net::FTP object if it can connect to the server, but returns "undef" if no connection is established.

"undef" does not have a "login" method, because it is not a valid Net::FTP object.


use something like

if ( defined $ftp ) {
$ftp->login($user,$passwd) or $newerr=1;
} else {
&log("no server connection - cannot login to FTP server, host = $host");
}
0
 

Author Comment

by:budihartono
ID: 34913287
HI Don,

Thank you for your help.
But if i checked on the script, it doesnt say anything much about the failed reason.
I guess if the script is failed, it will only log no server connection.

The things that i want to know is why it's failed ?Is it network connection or failed port? or something else? So actually we wan to retrieve information what is this 'undef' ?

Since sometimes it works, sometimes it doesnt work.

Thank you.
0
 
LVL 14

Expert Comment

by:DonConsolio
ID: 34913350
"If the constructor fails undef will be returned and an error message will be in $@"

&log("no server connection - cannot login to FTP server, host = $host, reason: $@");

0
 
LVL 24

Assisted Solution

by:mankowitz
mankowitz earned 125 total points
ID: 34913361
try setting debug to 1 when you make the connection, and make sure to watch STDERR

    $ftp = Net::FTP->new("some.host.name", Debug => 1)
      or die "Cannot connect to some.host.name: $@";
0
 

Author Comment

by:budihartono
ID: 34913445
Hi all,

Thanks..
now i can see it why ,
2011-02-17 12:03:00 [20770] Trying to get new FTP connection
2011-02-17 12:05:00 [20770] Cannot connect to 10.159.5.2 : Net::FTP: Timeout
2011-02-17 12:05:00 [20770] no server connection - cannot login to FTP server, host = 10.159.5.2, reason: Net::FTP: Timeout

But i am teriblle sory, my next question is, whether this is a network problem ? How do we check it? Can we add some ping command ?
 But if i check while the script is trying to get FTP connection, manually i check using ping, i got result that the ip is alive.

I am so confused.
Thank you.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:budihartono
ID: 34913449
secondly..right after the crontab is running the perl script, and the result is Time Out, then i mannually running the script, it works ...!!

I was wondering why from crontab is failed.
0
 
LVL 14

Expert Comment

by:DonConsolio
ID: 34913494
try increasing the Timeout parameter

$ftp=Net::FTP->new($host,Debug => 1, Timout => 300) or $newerr=1;
0
 
LVL 14

Expert Comment

by:DonConsolio
ID: 34913497
oops - Timeout it is :-)

$ftp=Net::FTP->new($host,Debug => 1, Timeout => 300) or $newerr=1;
0
 

Author Comment

by:budihartono
ID: 34923310
Hi,

I try to add longer timeout as suggested, the result, stil the same, sometimes its working, sometimes it is not working.
THe log is
2011-02-17 15:07:44 [3091] Trying to get new FTP connection
2011-02-17 15:11:29 [3091] Cannot connect to SGJKT4 : Net::FTP: Timeout
2011-02-17 15:11:29 [3091] no server connection - cannot login to FTP server, host = SGJKT4, reason: Net::FTP: Timeout

2011-02-17 16:03:00 [16029] Trying to get new FTP connection
2011-02-17 16:06:45 [16029] Cannot connect to 10.159.5.2 : Net::FTP: Timeout
2011-02-17 16:06:45 [16029] no server connection - cannot login to FTP server, host = 10.159.5.2, reason: Net::FTP: Timeout


As your information, i have 4 line of the FTP script that executed by crontab.

But still i don't understand, sometimes it works, sometimes it's not.
even when it's trying to get ftp connection , mannualy i try to ping, i can reach the server.
Also when i run it manually, its always working.

Weird..


0
 
LVL 14

Accepted Solution

by:
DonConsolio earned 125 total points
ID: 34930614
Try using the Net::Ping module with (with the tcp or udp option if you cannot run as root)
before you try to FTP.


http://perldoc.perl.org/Net/Ping.html

use Net::Ping;

$host = "10.159.5.2";
$p = Net::Ping->new();
print "$host is alive.\n" if $p->ping($host);
$p->close();

Open in new window

0
 
LVL 9

Expert Comment

by:Suhas .
ID: 37319317
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now