sarshads has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to execute the perl script copying remote files to local machine using FTP::Net Module. Remote machine is solaris and local machine is Windows. From the following Script, I can able to login successfully but cannot copy files to local machine(indicating that 0 files trasferred). Please let me know what is the mistakes from the following script:
use strict; use warnings; use Net::FTP; use Net::Netrc; my $remote_cwd = "/test"; my $local_cwd="C:"; my $server="100.200.100.100"; my $ftpid = "abc"; my $pass = "def"; my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3); $ftp->login("$ftpid","$pass"); $ftp->cwd($remote_cwd); my @files = $ftp->ls($remote_cwd); for (@files) { $ftp->get($_) if -f $_; print "\n".$_; } $ftp->quit;
Thanks.

Regards

Syed Arshad

  • Comment on Want to Copy files from remote machine(Solaris) to Local Machine(Windows) using FTP::Net
  • Download Code

Replies are listed 'Best First'.
Re: Want to Copy files from remote machine(Solaris) to Local Machine(Windows) using FTP::Net
by JavaFan (Canon) on Oct 05, 2010 at 11:05 UTC
    You don't check the return values of login, nor of cwd. Yet you assume they succeed. Perhaps you should make sure they succeed before questioning the return values of later commands.
Re: Want to Copy files from remote machine(Solaris) to Local Machine(Windows) using FTP::Net
by kcott (Archbishop) on Oct 05, 2010 at 13:09 UTC

    There's a number of potential problems here:

    • You've defined $local_cwd but haven't used it.
    • $ftp->ls() does not return a list of files. It will be more like a list of lines returned by ls -l on your Solaris box or dir on your Windows box.
    • Do you really only want to get files you already have: if -f $_

    The documentation for Net::FTP explains what ls() and the other methods do. It also has four examples of checking return values (in the SYNOPSIS) as correctly recommended in the previous post.

      Thank both of you. I realiaze my mistake and follow the link as given you. I successfully retrieve file to my directory :
      use strict; use warnings; use Net::FTP; use Net::Netrc; my $ftp; $ftp = Net::FTP->new ("100.200.100.200", Timeout => 9000, Debug => 3) or die "Cannot connect to some.host.name: $@"; $ftp->login("abc",'def') or die "Cannot login ", $ftp->message; $ftp->cwd("/test") or die "Cannot change working directory ", $ftp->message; $ftp->get("original.pl") or die "get failed ", $ftp->message; $ftp->quit;
      Thanks
Re: Want to Copy files from remote machine(Solaris) to Local Machine(Windows) using FTP::Net
by Khen1950fx (Canon) on Oct 05, 2010 at 17:49 UTC
    To download multiple files, Net::FTP won't really help you that much. To download a directory or multiple directories, use Net::DownloadMirror. Here's an example downloading a directory(3272K):
    #!perl use strict; use warnings; use Net::FTP; use Net::DownloadMirror; use constant HOST => 'ftp.cpan.org'; use constant REMOTE => '/pub/xemacs-www/Download/'; use constant LOCAL => '/root/Desktop/local'; use constant USER => 'anonymous'; use constant PASS => '-anonymous@'; my $ftp = Net::DownloadMirror->new( ftpserver => HOST, user => USER, pass => PASS, debug => 1, localdir => LOCAL, remotedir => REMOTE, ); $ftp->Download();