Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Net::FTP get problem (unexpected EOF)

by theirpuppet (Sexton)
on Jun 18, 2002 at 05:23 UTC ( #175281=categorized question: print w/replies, xml ) Need Help??
Contributed by theirpuppet on Jun 18, 2002 at 05:23 UTC
Q&A  > network programming


I just can't seem to get a file from an FTP server using Net::FTP. I've got the latest version from CPAN. here's my code:
sub retrieve_file { my($file) = shift; my($site) = shift; my($user) = shift; my($pass) = shift; my($dir) = shift; my($params_ref) = shift; my($pwd) = (); my($retval) = 0; chomp($pwd = `/bin/pwd`); chdir($TOP_DIR); chdir($$params_ref{FILE_DIR}); my($ftp) = Net::FTP->new($site, Debug => 1) or warn_msg("retri +eve_file: couldn't start FTP session for $site: $!") && return 0; $ftp->login($user, $pass) or warn_msg("retrieve_file: couldn't + access $site: " . $ftp->message()) && return 0; $ftp->cwd($dir) or warn_msg("retrieve_file: couldn't access $d +ir on $site: " . $ftp->message()) && return 0; if ($file =~ /\//) { my(@dirs) = split(/\//,$file); foreach (@dirs) { if ($_ eq $dirs[$#dirs]) { $file = $_; last; } $ftp->cwd($_) or warn_msg("retrieve_file: coul +dn't access $_ on $site: " . $ftp->message()) && return 0; } } $ftp->binary(); $ftp->get($file, $file) or warn_msg("retrieve_file: couldn't g +et $file: " . $ftp->message()) && return 0; $ftp->quit; chdir($pwd); return 1; }
here's my error message: Net::FTP: Unexpected EOF on command channel at ./ line 84

Or, I get no error message, and simply nothing happens.
Is there something weird that I should be doing, like read'ing a filehandle? Or anything else? Or is Net::FTP just broken for me?

Answer: Net::FTP get problem
contributed by rob_au

While there's not quite enough here for me to be sure, my strong guess is that you are experiencing the same problem as described here - If indeed, you are facing the same issue, it is the result of not so much your script, but your network configuration and specifics of the FTP protocol.

When connecting to a FTP server, two socket streams are used for communications - The first is the control connection initiated by your FTP client between which the FTP client and server exchange commands and replies (TCP socket 21). The second is a full-duplex connection over which data is transferred in a specified mode and type. The data transferred may be part of a file, an entire file or a listing of files within a directory. Typically, this data port is the port adjacent to the control port (TCP socket 20).

In general, it is the FTP server's responsibility to initiate and maintain the data connection. It is here that your script is running into problems.

This host address, along with the preferred socket for data communications, are sent to the server via the PORT command. I suspect that you are in fact connecting to a live Internet host with this script through a network firewall or layer of address translation and as such, the data connection from the server to your machine is failing because the FTP server is unable to reach your private machine.

The means by which to fix this is to switch to passive FTP transfer mode via the PASV command - This command changes the default behaviour of data port negotiation, shifting the onus for responsibility for data port establishment and maintenance back to the client. This allows the client to control its only data connection through the network firewall or translation layer, allowing normal FTP communications to occur. eg.

use Net::FTP; # Passive mode can be set with object initiation my $ftp = Net::FTP->new( $hostname, Debug => 1, Passive => 1 ) or die +$!; $ftp->login( $user, $password ); # Or with $object->pasv; $ftp->pasv;

Further information on this topic can be found in RFC documents (0959) File Transfer Protocol, (1122) Requirements for Internet hosts - communication layers and (1579) Firewall-Friendly FTP.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (8)
    As of 2020-09-18 20:58 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (113 votes). Check out past polls.