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:erl;
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";
}
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:erl;
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";
}