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

I'm trying to get a simple file download script working using Net::FTP. Here's my script:

#!/usr/bin/perl print "Content-type: text/plain\n\n"; use strict; use warnings; use Net::FTP; my $host ='192.168.0.110'; my $user ='root'; my $pw ='xxxxxx'; my $file ='hello.pl'; my $path ='/home/admin/temp'; my $ftp = Net::FTP->new($host, Debug => 1) or die "Could not connect to '$host': $@"; $ftp->login($user, $pw) or die print "Could not login1: %s", $ftp->message; $ftp->cwd($path) or die print "Could not change directory to %spath", $ftp->message +; $ftp->get("hello.pl") or die print "Could not get $file from $path ", $ftp->message; print "End";
The script seems to run and in fact I can successfully modify it to make and remove directories on the server. What I can't do is get it to download from the server to a local file. Server permissions are set properly, the file exists and the local directory exists. What I get is this error line, and it never gets to the terminating print line.: "Could not get hello.pl from /home/admin/temp Transfer complete. No transfer to ABOR." Can someone point me in the right direction? Joe

Replies are listed 'Best First'.
Re: Net::FTP get problem
by poj (Abbot) on Jul 28, 2017 at 18:53 UTC

    Why do you have this line, is this a web server run script ?

    print "Content-type: text/plain\n\n";

    Try listing the contents of the directory

    #!/usr/bin/perl use strict; use warnings; use Net::FTP; my $host ='192.168.0.110'; my $user = 'user'; my $pw = 'password'; my $path ='/home/admin/temp'; my $ftp = Net::FTP->new($host, Debug => 1) or die "Could not connect to '$host': $@"; $ftp->login($user, $pw) or die sprintf "Could not login: %s", $ftp->message; $ftp->cwd($path) or die sprintf "Could not cwd to %s", $ftp->message; printf "Current dir = %s\n",$ftp->pwd(); print join "\n",$ftp->dir(); print "End";
    poj
      poj Yes, this script runs on an Apache web server in an embedded Linux system. I've done a directory listing on the source directory and the requested file is there. Permissions are OK.

        What do you get adding this line to your script after the $ftp->cwd($path) ?

        print join "\n",$ftp->dir();

        Try changing the destination path to /tmp

        $ftp->get($file,"/tmp/$file") or die sprintf "Could not get $file from $path ", $ftp->message;

        poj
Re: Net::FTP get problem
by roboticus (Chancellor) on Jul 28, 2017 at 21:47 UTC

    joedarock:

    If you can create directories on the server but have trouble getting a file, I'd suspect either:

    • Permissions on one side or other of the connection, or
    • Restrictions on FTP -- have you tried passive mode?

    Other than those, I can't think of anything in particular. It would be helpful to see some diagnostics.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      roboticus Permissions on both ends look OK. The "join" line prints a directory listing w/permissions. I'm new to Perl...can you provide guidance on how to get some diagnostics that might be useful to you?

        joedarock:

        Have you tried manually doing the FTP through an FTP client? It might be helpful to verify that you can fetch the file manually to help narrow down where the problem could be.

        As far as collecting more diagnostic information, you're already telling the FTP object to report debug information. Perhaps you can increase the debug level to get more details.

        Have you tried using the Passive option? Some servers/firewalls (I don't recall which) won't perform file transfers except via Passive mode.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        What are the permissions on the directory that contains your script. Try this script in the same directory as your script to check the directory is writable to by your webserver

        #!/usr/bin/perl # testwrite.pl use strict; use Cwd; print "Content-type: text/plain\n\n"; my $cwd = cwd(); if ( open my $fh,'>',$cwd.'/testwrite' ){ print "OK $cwd is writable" } else { print "ERROR $cwd is NOT writable" }
        poj