Net::FTP(3perl) | Perl Programmers Reference Guide | Net::FTP(3perl) |
Net::FTP - FTP Client class
use Net::FTP; $ftp = Net::FTP->new("some.host.name", Debug => 0) or die "Cannot connect to some.host.name: $@"; $ftp->login("anonymous",'-anonymous@') or die "Cannot login ", $ftp->message; $ftp->cwd("/pub") or die "Cannot change working directory ", $ftp->message; $ftp->get("that.file") or die "get failed ", $ftp->message; $ftp->quit;
"Net::FTP" is a class implementing a simple FTP client in Perl as described in RFC959. It provides wrappers for the commonly used subset of the RFC959 commands. If IO::Socket::IP or IO::Socket::INET6 is installed it also provides support for IPv6 as defined in RFC2428. And with IO::Socket::SSL installed it provides support for implicit FTPS and explicit FTPS as defined in RFC4217.
The Net::FTP class is a subclass of Net::Cmd and (depending on avaibility) of IO::Socket::IP, IO::Socket::INET6 or IO::Socket::INET.
FTP stands for File Transfer Protocol. It is a way of transferring files between networked machines. The protocol defines a client (whose commands are provided by this module) and a server (not implemented in this module). Communication is always initiated by the client, and the server responds with a message and a status code (and sometimes with data).
The FTP protocol allows files to be sent to or fetched from the server. Each transfer involves a local file (on the client) and a remote file (on the server). In this module, the same file name will be used for both local and remote if only one is specified. This means that transferring remote file "/path/to/file" will try to put that file in "/path/to/file" locally, unless you specify a local file name.
The protocol also defines several standard translations which the file can undergo during transfer. These are ASCII, EBCDIC, binary, and byte. ASCII is the default type, and indicates that the sender of files will translate the ends of lines to a standard representation which the receiver will then translate back into their local representation. EBCDIC indicates the file being transferred is in EBCDIC format. Binary (also known as image) format sends the data as a contiguous bit stream. Byte format transfers the data as bytes, the values of which remain the same regardless of differences in byte size between the two machines (in theory - in practice you should only use this if you really know what you're doing). This class does not support the EBCDIC or byte formats, and will default to binary instead if they are attempted.
"HOST" is optional. If "HOST" is not given then it may instead be passed as the "Host" option described below.
"OPTIONS" are passed in a hash like fashion, using key and value pairs. Possible options are:
Host - FTP host to connect to. It may be a single scalar, as defined for the "PeerAddr" option in IO::Socket::INET, or a reference to an array with hosts to try in turn. The "host" method will return the value which was used to connect to the host.
Firewall - The name of a machine which acts as an FTP firewall. This can be overridden by an environment variable "FTP_FIREWALL". If specified, and the given host cannot be directly connected to, then the connection is made to the firewall machine and the string @hostname is appended to the login identifier. This kind of setup is also referred to as an ftp proxy.
FirewallType - The type of firewall running on the machine indicated by Firewall. This can be overridden by an environment variable "FTP_FIREWALL_TYPE". For a list of permissible types, see the description of ftp_firewall_type in Net::Config.
BlockSize - This is the block size that Net::FTP will use when doing transfers. (defaults to 10240)
Port - The port number to connect to on the remote machine for the FTP connection
SSL - If the connection should be done from start with SSL, contrary to later upgrade with "starttls".
SSL_* - SSL arguments which will be applied when upgrading the control or data connection to SSL. You can use SSL arguments as documented in IO::Socket::SSL, but it will usually use the right arguments already.
Timeout - Set a timeout value in seconds (defaults to 120)
Debug - debug level (see the debug method in Net::Cmd)
Passive - If set to a non-zero value then all data transfers will be done using passive mode. If set to zero then data transfers will be done using active mode. If the machine is connected to the Internet directly, both passive and active mode should work equally well. Behind most firewall and NAT configurations passive mode has a better chance of working. However, in some rare firewall configurations, active mode actually works when passive mode doesn't. Some really old FTP servers might not implement passive transfers. If not specified, then the transfer mode is set by the environment variable "FTP_PASSIVE" or if that one is not set by the settings done by the libnetcfg utility. If none of these apply then passive mode is used.
Hash - If given a reference to a file handle (e.g., "\*STDERR"), print hash marks (#) on that filehandle every 1024 bytes. This simply invokes the "hash()" method for you, so that hash marks are displayed for all transfers. You can, of course, call "hash()" explicitly whenever you'd like.
LocalAddr - Local address to use for all socket connections. This argument will be passed to the super class, i.e. IO::Socket::INET or IO::Socket::IP.
Domain - Domain to use, i.e. AF_INET or AF_INET6. This argument will be passed to the IO::Socket super class. This can be used to enforce IPv4 even with IO::Socket::IP which would default to IPv6. Family is accepted as alternative name for Domain.
If the constructor fails undef will be returned and an error message will be in $@
Unless otherwise stated all methods return either a true or false value, with true meaning that the operation was a success. When a method states that it returns a value, failure will be returned as undef or an empty list.
"Net::FTP" inherits from "Net::Cmd" so methods defined in "Net::Cmd" may be used to send commands to the remote FTP server in addition to the methods documented here.
If the connection is via a firewall then the "authorize" method will be called with no arguments.
Returns most significant digit of the response code.
Hint: If both server and client machines use the same line ending for text files, then it will be faster to transfer all files in binary mode.
Returns the full pathname to the new directory.
The size of the file will be determined, and sent to the server automatically for normal files so that this method need only be called if you are transferring data from a socket, named pipe, or other stream not associated with a normal file.
In an array context, returns a list of lines returned from the server. In a scalar context, returns a reference to a list.
In an array context, returns a list of lines returned from the server. In a scalar context, returns a reference to a list.
If "WHERE" is given then the first "WHERE" bytes of the file will not be transferred, and the remaining bytes will be appended to the local file if it already exists.
Returns "LOCAL_FILE", or the generated local file name if "LOCAL_FILE" is not given. If an error was encountered undef is returned.
Returns "REMOTE_FILE", or the generated remote filename if "REMOTE_FILE" is not given.
NOTE: If for some reason the transfer does not complete and an error is returned then the contents that had been transferred will not be remove automatically.
Returns the name of the file on the server.
Returns "REMOTE_FILE", or the generated remote filename if "REMOTE_FILE" is not given.
NOTE: The size reported is the size of the stored file on the remote server. If the file is subsequently transferred from the server in ASCII mode and the remote server and local machine have different ideas about "End Of Line" then the size of file on the local machine after transfer may be different.
if ($ftp->feature( 'MDTM' )) { # Do something } if (grep { /\bTLS\b/ } $ftp->feature('AUTH')) { # Server supports TLS }
The following methods can return different results depending on how they are called. If the user explicitly calls either of the "pasv" or "port" methods then these methods will return a true or false value. If the user does not call either of these methods then the result will be a reference to a "Net::FTP::dataconn" based object.
If for some reason you want to have complete control over the data connection, this includes generating it and calling the "response" method when required, then the user can use these methods to do so.
However calling these methods only affects the use of the methods above that can return a data connection. They have no effect on methods "get", "put", "put_unique" and those that do not require data connections.
The following methods can be used to transfer files between two remote servers, providing that these two servers can connect directly to each other.
Returns most significant digit of the response code.
WARNING This call should only be used on commands that do not require data connections. Misuse of this method can hang the connection.
Some of the methods defined in "Net::FTP" return an object which will be derived from the "Net::FTP::dataconn" class. See Net::FTP::dataconn for more details.
The following RFC959 commands have not been implemented:
When reporting bugs/problems please include as much information as possible. It may be difficult for me to reproduce the problem as almost every setup is different.
A small script which yields the problem will probably be of help. It would also be useful if this script was run with the extra options "Debug => 1" passed to the constructor, and the output sent with the bug report. If you cannot include a small script then please include a Debug trace from a run of your program which does yield the problem.
Graham Barr <gbarr@pobox.com>.
Steve Hay <shay@cpan.org> is now maintaining libnet as of version 1.22_02.
Net::Netrc, Net::Cmd, IO::Socket::SSL
ftp(1), ftpd(8), RFC 959, RFC 2428, RFC 4217 http://www.ietf.org/rfc/rfc959.txt http://www.ietf.org/rfc/rfc2428.txt http://www.ietf.org/rfc/rfc4217.txt
For an example of the use of Net::FTP see
Henry Gabryjelski <henryg@WPI.EDU> - for the suggestion of creating directories recursively.
Nathan Torkington <gnat@frii.com> - for some input on the documentation.
Roderick Schertler <roderick@gate.net> - for various inputs
Copyright (C) 1995-2004 Graham Barr. All rights reserved.
Copyright (C) 2013-2017 Steve Hay. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e. under the terms of either the GNU General Public License or the Artistic License, as specified in the LICENCE file.
2020-07-21 | perl v5.28.1 |