Getting error text outof PERL's $sftp->get or do {...} - perl

I'm not a perl programmer but need to debug an error. I'm using the Net:SFTP:Foreign package.
When I attempt to get files, the following call fails:
$sftp->get(source, destination) or do { print "something went wrong."}
This line returns "something went wrong." What I would like is to find out WHAT went wrong! How can I extract the reason for failure?
By the way, this script has been working for months without an error. The script is very reliable, I just don't know how to capture the reason for failure.

$sftp->get(source, destination) or warn "get() failed with " . $sftp->error . "\n";

$sftp->get($source, $destination)
or print "something went wrong: " . $sftp->error . "\n";

Related

perl timeout when downloading file from ftp

I try to download huge file which takes a lot of time downloading from ftp link using perl.
I got:
Timeout at C:/Strawberry/perl/lib/Net/FTP.pm
what does this means and how to solve it?
Thanks
Solution:
Thanks #Chris Doyle
I change the timeout value in my perl file "not ftp.pm file"
Thanks
You can increase the timeout, but it is important that if the timeout is reached again and your server/client are out of sync, it might throw the same error you got the first time, again.
It seems that the issue is due a lack of error handling in your Perl Script instead.
Surely you have something like this at your perl script:
my $ftp = Net::FTP->new( $myhost, Timeout => 10, Debug => 1 );
...
$ftp->get($myfile) or print "Got an error";
$ftp->quit();
Please notice this is out of .../perl/lib/Net/FTP.pm, since the
FTP.pm is the third party module (Kind of library) you are using to
reach the ftp, I suggest you to not touch it to avoid portability
issues later on.
Normally the timeout is reached inside the FTP.pm and it goes to the or print "Got an error" condition, but there are some cases, that the Server/Client just get out of sync and the FTP.pm just throws an unhandled exception.
This exception will NOT go to the or print "Got an error" condition, therefore you need to catch it and handle it as any other languages.
Here you can use eval to wrap it up the code, catch the exception and handle it as you need.
For example:
my $ftp = Net::FTP->new( $myhost, Timeout => 10, Debug => 1 );
...
eval {$ftp->get($myfile) or print("Can't get file $myfile") };
if ($# =~ /Timeout/) {
print "Got a timeout Issue: $#";
}
$ftp->quit();

Download multiple files from SFTP using perl Net::SFTP::Foreign

I am using the below code to download files from SFTP and I am getting error message after one file download. Then I am rerunning the program and then it downloads only one file again and when trying to download the next file in the array it get failed. I couldn't understand the reasons for this failure and kindly review the below code and share opinions.
#!/usr/bin/perl
use strict;
use Net::SFTP::Foreign::Constants qw(:error :status);
use Net::SFTP::Foreign;
use Net::SFTP::Foreign::Attributes;
my $SFTP_CON=Net::SFTP::Foreign->new("sftpserver.test.com",user=>"testuser",password=>"pass");
my #SFTP_Files=('/test/1.txt','/test/2.txt','/test/3.txt');
my $root_dir='/test/';
$local_dir='/u/test/';
foreach my $file_to_dwn(#SFTP_Files){
my $file_to=$file_to_dwn;
$file_to=~s/$root_dir/$local_dir/igs;
unless($SFTP_CON->get("$file_to_dwn","$file_to")){
if (($SFTP_CON->error == SFTP_ERR_REMOTE_STAT_FAILED or
$SFTP_CON->error == SFTP_ERR_REMOTE_OPEN_FAILED) and
$SFTP_CON->status == SSH2_FX_NO_SUCH_FILE) {
print "Remote file does not exist!";
exit;
}
else {
print "Transfer Failed";
exit;
}
print $SFTP_CON->error."\n";
exit;
}
}
I am getting "Transfer Failed", when the program tries to download the one after another (second file download).
Please do needful.

perl script to access sec edgar master files returns file not found when file exists on ftp server

It would be great if someone can help. I really am stuck.
I am downloading the master files from SEC edgar and I got the script from—http://brage.bibsys.no/bi/bitstream/URN:NBN:no-bibsys_brage_38213/1/Norli_SRFE_2012.pdf (page 14..published now)
I get the error 404 master.gz not found
While debugging i made it paste the url and when i use the same in browser I can download the file. It is parsing the url correctly till QTR1 but after that it is not able to find the file when it actually exists ..please help.
1) for debugging reasons now I changed the code to 1995 (but later plan to add years 1995 to 2012)
2) It did not work for any file. When I said QTR1 abovr - I meant that the same code without the file name (just for testing ) -- ....full-index/1995/QTR1/ (without the file name) returns a status code OK but ...ftp.sec.gov/edgar/full-index/1995/QTR1/master.gz returns 404 file not found error. It does not work for any quarter.
I wasted so much time on this seemingly simple thing which is supposed to work but it is just not working.... could you copy past this and run..is it working for you?
The code below gets the master files from QTR folders. Pasting my code ::
—————-
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(250);
$ua->env_proxy;
for($year=1995; $year<1996; $year=$year+1){
for($i=1; $i<5; $i=$i+1){
$quarter = “QTR” . $i;
$filegrag = “ftp://ftp.sec.gov/edgar/full-index/” . $year . “/” . $quarter . “/master.gz”;
print $filegrag;
# This command gets the file from EDGAR
my $response = $ua->get($filegrag);
print $response;
print $response->status_line;
# Now just pipe the output to a file named appropriately
$filename = $year . $quarter . “master”;
open(MYOUTFILE, “>” . $filename);
if ($response->is_success) {
print MYOUTFILE $response->decoded_content;
}
else {
die $response->status_line;
}
close(MYOUTFILE);
}
}
I realized that there were some firewall issues that were causing the problem I had. Now things are fine.

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

#!/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.

Open2 Api failure while installing Mead software

I am running into some issues trying to install a software called MEAD . I would appreciate if someone could have alook .
I get the following error while installing
/mead/bin # ./mead.pl GA3
Using system rc-file: /home/karosh/mead/bin/../.meadrc
Warning: Can't find user rc-file
Cluster: /home/karosh/mead/bin/../data/GA3/GA3.cluster
open2: exec of /home/karosh/mead/bin/driver.pl failed at ./mead.pl line 230
THe mead software is not written by me so I have not changed any of the perl scrips . I line 230 in the driver.pl file is
sub run_mead {
my %options = #_;
my $reader = FileHandle->new();
my $writer = FileHandle->new();
unless ( open2($reader, $writer, "$FindBin::Bin/driver.pl") ) {
die "Unable to run MEAD.\n";
}
...
...
}
Does this error mean that open2 was not found . The mead folks have put the following line in the file:
use strict;
use File::Spec;
use FileHandle;
use IPC::Open2;
Or does it mean that i need to install the rpm that contains the API . I see that this API is a part of the core perl bundle http://perldoc.perl.org/IPC/Open2.html. So why was it not installed ? Do i need to install perl again .
Someone has earlier faced this problem - http://www.summarization.com/~radev/mead/email/0160.html but the solution is not working for me . I find no Perl files with the incorrect perl directives . The mead team has been dissolved and there is no one to ask questions but I need to use this software.
I think if some one can explain me the meaning of the error than I can do deeper. Anyone?
It probably means that .../driver.pl doesn't have execute permission. Change the file permissions or call it like
open2($reader, $writer, "perl $FindBin::Bin/driver.pl")
open2($reader, $writer, "$^X $FindBin::Bin/driver.pl")