net::SFTP to capture 'get' reference?

R

Rob

Hi,

I'm sftp'ing files from one machine to this one, and wanted to
capture the error messages for the $sftp->get reference.

I tried the eval method, but that didn't work...any ideas?
here's my script.

#!/usr/bin/perl

select(STDERR); $| = 1; # flush output buffer (STDERR)
select(STDOUT); $| = 1; # flush output buffer (STDOUT)


use Net::SSH::perl;
use Net::SFTP;
use Net::SFTP::Util;

$user = 'username';
$passwd = 'password';
$site = 'machine1';
$data = 'result';

eval {
$sftp = Net::SFTP->new($site,user => $user, password => $passwd, debug
=> 0);
};
if ($@) {
print "SFTP connection to $site failed: $@\n";
}

if ($sftp) {
print "Connected to $site\n";
} else {
print "cannot connect\n";
exit;
}

$dir = "/home/username/";
$odir = "/tmp";
eval {
@array=$sftp->ls($dir);
};
if ($@) {
print "cannot do ls ($dir): $@\n";

}

foreach (@array) {
$file = $_;
($size,$filename) = (split(/\s+/,$file->{longname}))[4,8];
$file{$filename} = $size;
}

foreach $f (sort keys(%file)) {
print "remote found $f\n";
next if ($f !~ /^$data/);
print "$f ($file{$f})\n";


####HERE IS THE AREA OF INTEREST######
#if i put a 'a' in front of $f to cause $sftp->get
#to fail, I want to capture it & continue with the
#script rather it killing it.
eval {
$sftp->get("$dir/$f","$odir/$f", \&callback);
};
if ($@) {
print "Could not get $dir/$f\n";
print "error is: $@\n";
}
# $a = $sftp->do_remove("$dir/$f");
# $b = fx2txt($a);
#print "$a, $b,\n";
#last;
}

sub callback {
my($sftp, $data, $offset, $size) = @_;
print "Retrieved $f $offset of $size,\n";
}
 
B

Brian McCauley

Rob said:
I'm sftp'ing files from one machine to this one, and wanted to
capture the error messages for the $sftp->get reference.

I tried the eval method, but that didn't work...any ideas?
here's my script.

What version of Perl are you using. I have seen cases in 5.6 where eval
{} will trap an error and fail to set $@. I don't thing I seen this
happen in 5.8.
 
R

Rob

Ah, sorry, perhaps I should have been more clear.

I'm running perl 5.8.0 on a redhat WS3 machine.
when I run the script, it prints the error message
to the STDOUT:
Couldn't stat remote file: No such file or directory at
/usr/lib/perl5/site_perl/5.8.0/Net/SFTP.pm line 130.

I want this in $x so I can call fx2txt funcion within
SFTP::Util pm.
 
J

Jussi Mononen

I'm running perl 5.8.0 on a redhat WS3 machine.
when I run the script, it prints the error message
to the STDOUT:
Couldn't stat remote file: No such file or directory at
/usr/lib/perl5/site_perl/5.8.0/Net/SFTP.pm line 130.

I want this in $x so I can call fx2txt funcion within
SFTP::Util pm.

If the status method
(http://search.cpan.org/~dbrobins/Net-SFTP-0.09/lib/Net/SFTP.pm#$sftp->status)
does not help you, you could try to use a localised signal handler for
warnings to capture them into a variable since some SFTP functions warn()
you when they fail.

eg.

{
my $warnings;
local $SIG{__WARN__} = sub { chomp; $warnings .= $_; };

if( ! $sftp-get($from, $to, \&callback) {
print "Unable to get $from : $warnings\n";
}
}

/jUSSi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top