Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Download file from sftp or FTP

by gube (Parson)
on Dec 28, 2009 at 17:51 UTC ( #814638=perlquestion: print w/ replies, xml ) Need Help??
gube has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I want to download file from sftp. If i hardcoded the filename it works. But, the file starts is random.

Can some one help me why it fails ?

Code below :
Works : $data_file = "Decemeber2009_3rdParty.csv"; Fails : $data_file = "*_3rdParty.csv"; SFTP : my %args = (user => encode("iso-8859-1", $ftp_user) , password => +encode("iso-8859-1", $ftp_passwd)); $args{ssh_args} = [ port => encode("iso-8859-1", $ftp_port) ] + if $ftp_port; $sftp = Net::SFTP->new($ftp_url, %args); return 0 if (!$sftp); $sftp->get(encode("iso-8859-1", $ftp_file), $local_file); my ($statuscode, $statustext) = $sftp->status; $log->error("\nFile not found") if($statustext ne "No error"); FTP: $ftp = ($ftp_port) ? Net::FTP->new($ftp_url, Port => $ftp_port) : +Net::FTP->new($ftp_url); return 0 if (!$ftp); $ftp->login($ftp_user, $ftp_passwd) or return 0; $ftp->get($ftp_file, $local_file) or $log->error("\nFile not f +ound"); $ftp->quit;

Thanks

Comment on Download file from sftp or FTP
Download Code
Re: Download file from sftp or FTP
by almut (Canon) on Dec 28, 2009 at 18:14 UTC

    The get method does not do wildcard expansion, which is why "*_3rdParty.csv" does not work.

    Unfortunately, Net::FTP doesn't have an mget command/method (like many system ftp tools), so you'd have to roll one yourself. Something like this (untested):

    $ftp->get($_) for grep /_3rdParty\.csv$/, $ftp->ls();
Re: Download file from sftp or FTP
by gube (Parson) on Dec 28, 2009 at 18:29 UTC

    Thanks almet for ftp idea. Same, I got inputs from the node http://www.perlmonks.org/?node_id=539261 for SFTP. I got the files what i need from the directory using $sftp->ls.

    my %args = (user => encode("iso-8859-1", $ftp_user) , password => enco +de("iso-8859-1", $ftp_passwd)); $args{ssh_args} = [ port => encode("iso-8859-1", $ftp_port) ] + if $ftp_port; $sftp = Net::SFTP->new($ftp_url, %args); return 0 if (!$sftp); $sftp->ls($ftp_additional_path, sub { my $ref = shift; if ($ref->{'filename'} = +~ /_3rdParty_/) { $files->{$ref->{'fil +ename'}} = 1 } }); print (keys %{$files}); $sftp->get(encode("iso-8859-1", $ftp_file), $local_file); my ($statuscode, $statustext) = $sftp->status; $log->error("\nFile not found") if($statustext ne "No error");
Re: Download file from sftp or FTP
by salva (Monsignor) on Dec 28, 2009 at 23:15 UTC
    using Net::SFTP::Foreign:
    use File::Spec; use Net::SFTP::Foreign; my %args = (user => encode("iso-8859-1", $ftp_user), password => encode("iso-8859-1", $ftp_passwd), fs_encoding => "iso-8591-1"); $args{port} = encode("iso-8859-1", $ftp_port) if defined $ftp_port; $sftp = Net::SFTP::Foreign->new($ftp_url); $sftp->error and die "unable to connect ro remote host: " . $sftp->err +or; my @files = $sftp->glob($ftp_file, names_only => 1); $sftp->error and die "unable to glob: " . $sftp->error; for my $file (@files) { $file =~ m|([^/]+)$| or die "bad remote file name '$file'"; my $local = File::Spec->catfile($local_file, $1); $sftp->get($file, $local); $sftp->error and die "file transfer failed: " . $sftp->error; }
      And now...
      use Net::SFTP::Foreign; # BTW, setting your locale correctly would probably # make these "encode" operations unnecessary! my %args = (user => encode("iso-8859-1", $ftp_user), password => encode("iso-8859-1", $ftp_passwd), fs_encoding => "iso-8591-1"); $args{port} = encode("iso-8859-1", $ftp_port) if defined $ftp_port; $sftp = Net::SFTP::Foreign->new($ftp_url); $sftp->error and die "unable to connect ro remote host: " . $sftp->err +or; $sftp->mget($ftp_file, $local, on_error => sub { warn "error: " . $_[0]->error });

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://814638]
Approved by almut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2014-08-21 20:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (143 votes), past polls