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

Hi Experts, I am facing a issue with FTP section in my perl script. I am trying to get a list of log files from a mobile device. It has FTP server running. Here is the script =========================================
use File::Path; use File::Copy; use File::Basename; use Net::FTP; open(FILE, "<", "api_log_file.txt"); while(<FILE>) { chomp; push(@arr, $_); } close(FILE); chdir("C:/OzoneAPI3Regresssion/Logs"); print "Connecting to HH Device 192.168.2.35 ................ "; my $ftp=Net::FTP->new("192.168.2.35",Debug => 1,Passive => 0,Timeout=> +1000); if ( !$ftp ) { die "Cannot connect to the ftp server: $@\n"; } else { print "done\n"; } print "Passing credentials ....................... "; $ftp->login("admin","admin") or die "Could not login\n",$ftp->messag +e; print "done\n"; foreach(@arr) { print "array element: $_\n"; chomp($_); $ftp->binary; $ftp->get("$_") or die "Cannot get $_: $! $^E\n"; } print "Closing connection ........................ "; $ftp->quit; print "done\n"; ====================
When I run this I am getting the following error.
Net::FTP>>> Net::FTP(2.77) Net::FTP>>> Exporter(5.62) Net::FTP>>> Net::Cmd(2.29) Net::FTP>>> IO::Socket::INET(1.31) Net::FTP>>> IO::Socket(1.30_01) Net::FTP>>> IO::Handle(1.27) Net::FTP=GLOB(0x1f04884)<<< 220 FreeFloat Ftp Server (Version 1.00). Connecting to Ozone Device 192.168.2.35 ................ done Net::FTP=GLOB(0x1f04884)>>> USER admin Net::FTP=GLOB(0x1f04884)<<< 331 Password required for admin. Net::FTP=GLOB(0x1f04884)>>> PASS .... Net::FTP=GLOB(0x1f04884)<<< 230 User admin logged in. Passing credentials ....................... done array element: Access_Sequence_DotNet_Log.html Net::FTP=GLOB(0x1f04884)>>> PORT 192,168,2,31,14,49 Net::FTP=GLOB(0x1f04884)<<< 200 PORT command successful. Net::FTP=GLOB(0x1f04884)>>> RETR Access_Sequence_DotNet_Log.html Net::FTP=GLOB(0x1f04884)<<< 550 \Access_Sequence_DotNet_Log.html : can +'t find the file Cannot get Access_Sequence_DotNet_Log.html : Bad file descriptor
Can I get some assistance? Regards Amit

Replies are listed 'Best First'.
Re: Bad File descriptor Error
by Anonymous Monk on Jan 04, 2011 at 07:31 UTC
    You have

    $ftp->get("$_") or die "Cannot get $_: $! $^E\n";

    But $! and $^E aren't set by Net::FTP. The module's synopsis has a clear example of how to get the error message:

    $ftp->get("that.file") or die "get failed ", $ftp->message;

    Note that this'll expose that 550 error that the debug output shows, which means "file unavailable" according to a FTP RFC:

    550 Requested action not taken. File unavailable (e.g., file not found, no access).
      Thanks a lot for quick reply.
      Whatever file I am trying to access is available in that location.
      Strangely I see a "\" is being getting added to filename by "get()". I am just passing file name as it is. Does the function get() misbehave?
      Net::FTP=GLOB(0x1eb0c44)>>> PORT 192,168,2,31,14,171 Net::FTP=GLOB(0x1eb0c44)<<< 200 PORT command successful. Net::FTP=GLOB(0x1eb0c44)>>> RETR Access_Sequence_DotNet_Log.html Net::FTP=GLOB(0x1eb0c44)<<< 550 \Access_Sequence_DotNet_Log.html : can +'t find the file Cannot get Access_Sequence_DotNet_Log.html \Access_Sequence_DotNet_Log.html : can't find the file
      Thanks
      Amit
        I assume that the leading backslash indicates the full path the server thinks you're asking for. Note the RETR line shows the correct filename is sent.

        You might want to print $ftp->cwd(), "\n"; before you call ->get to make sure you're in the right directory on the server.

        Strangely I see a "\" is being getting added to filename by "get()"

        No, by the server. Note the direction of the arrows. "<<<" means from the server. You need to either specify an absolute path or set the cwd correctly.

Re: Bad File descriptor Error
by GrandFather (Saint) on Jan 04, 2011 at 07:12 UTC

    Could it be as simple as the file doesn't exist? Sure looks that way from the logging.

    True laziness is hard work
Re: Bad File descriptor Error
by samarzone (Pilgrim) on Jan 04, 2011 at 08:00 UTC
    am trying to get a list of log files from a mobile device

    get() tries to retrieve a file not the list of files. Why don't you use ls()?

    --
    Regards
    - Samar
      I need to download the log files from device using ftp So
      using get().Any other idea is welcome.
      Regards
      Amit

        It looks like the ftp server can't find the file you're requesting. What happens if you try to download the files listed in "api_log_file.txt" using a ftp client?

        edited: 20110104 12:57 GMT to remove spelling mistake

        I am not sure about the leading backslash problem but I have something to suggest.

        You have a file "api_log_file.txt" containing the names of files to be downloaded. You should use ls() to get the list of files and iterate over this list to download selected files instead of a list (@arr) from a different source.

        --
        Regards
        - Samar