NAME

Net::SFTP - Secure File Transfer Protocol client

SYNOPSIS

use Net::SFTP;
my $sftp = Net::SFTP->new($host);
$sftp->get("foo", "bar");
$sftp->put("bar", "baz");

DESCRIPTION

Net::SFTP is a pure-Perl implementation of the Secure File Transfer Protocol (SFTP)--file transfer built on top of the SSH protocol. Net::SFTP uses Net::SSH::Perl to build a secure, encrypted tunnel through which files can be transferred and managed. It provides a subset of the commands listed in the SSH File Transfer Protocol IETF draft, which can be found at http://www.openssh.com/txt/draft-ietf-secsh-filexfer-00.txt.

SFTP stands for Secure File Transfer Protocol and is a method of transferring files between machines over a secure, encrypted connection (as opposed to regular FTP, which functions over an insecure connection). The security in SFTP comes through its integration with SSH, which provides an encrypted transport layer over which the SFTP commands are executed, and over which files can be transferred. The SFTP protocol defines a client and a server; only the client, not the server, is implemented in Net::SFTP.

Because it is built upon SSH, SFTP inherits all of the built-in functionality provided by Net::SSH::Perl: encrypted communications between client and server, multiple supported authentication methods (eg. password, public key, etc.).

USAGE

Net::SFTP->new($host, %args)

Opens a new SFTP connection with a remote host $host, and returns a Net::SFTP object representing that open connection.

%args can contain:

  • user

    The username to use to log in to the remote server. This should be your SSH login, and can be empty, in which case the username is drawn from the user executing the process.

    See the login method in Net::SSH::Perl for more details.

  • password

    The password to use to log in to the remote server. This should be your SSH password, if you use password authentication in SSH; if you use public key authentication, this argument is unused.

    See the login method in Net::SSH::Perl for more details.

  • debug

    If set to a true value, debugging messages will be printed out for both the SSH and SFTP protocols. This automatically turns on the debug parameter in Net::SSH::Perl.

    The default is false.

  • ssh_args

    Specifies a reference to a list of named arguments that should be given to the constructor of the Net::SSH::Perl object underlying the Net::SFTP connection.

    For example, you could use this to set up your authentication identity files, to set a specific cipher for encryption, etc.

    See the new method in Net::SSH::Perl for more details.

$sftp->get($remote [, $local [, \&callback ] ])

Downloads a file $remote from the remote host. If $local is specified, it is opened/created, and the contents of the remote file $remote are written to $local. In addition, its filesystem attributes (atime, mtime, permissions, etc.) will be set to those of the remote file.

If $local is not given, returns the contents of $remote.

If \&callback is specified, it should be a reference to a subroutine. The subroutine will be executed at each iteration of the read loop (files are generally read in 8192-byte blocks, although this depends on the server implementation). The callback function will receive as arguments: a Net::SFTP object with an open SFTP connection; the data read from the SFTP server; the offset from the beginning of the file (in bytes); and the total size of the file (in bytes). You can use this mechanism to provide status messages, download progress meters, etc.:

sub callback {
    my($sftp, $data, $offset, $size) = @_;
    print "Read $offset / $size bytes\r";
}

$sftp->put($local, $remote [, \&callback ])

Uploads a file $local from the local host to the remote host, and saves it as $remote.

If \&callback is specified, it should be a reference to a subroutine. The subroutine will be executed at each iteration of the write loop, directly after the data has been read from the local file. The callback function will receive as arguments: a Net::SFTP object with an open SFTP connection; the data read from $local, generally in 8192-byte chunks;; the offset from the beginning of the file (in bytes); and the total size of the file (in bytes). You can use this mechanism to provide status messages, upload progress meters, etc.:

sub callback {
    my($sftp, $data, $offset, $size) = @_;
    print "Wrote $offset / $size bytes\r";
}

$sftp->ls($remote [, $subref ])

Fetches a directory listing of $remote.

If $subref is specified, for each entry in the directory, $subref will be called and given a reference to a hash with three keys: filename, the name of the entry in the directory listing; longname, an entry in a "long" listing like ls -l; and a, a Net::SFTP::Attributes object, which contains the file attributes of the entry (atime, mtime, permissions, etc.).

If $subref is not specified, returns a list of directory entries, each of which is a reference to a hash as described in the previous paragraph.

COMMAND METHODS

Net::SFTP supports all of the commands listed in the SFTP version 3 protocol specification. Each command is available for execution as a separate method, with a few exceptions: SSH_FXP_INIT, SSH_FXP_VERSION, SSH_FXP_READ, SSH_FXP_WRITE, SSH_FXP_READDIR.

These are the available command methods:

$sftp->do_open($path, $flags [, $attrs ])

Sends the SSH_FXP_OPEN command to open a remote file $path, and returns an open handle on success. On failure returns undef. The "open handle" is not a Perl filehandle, nor is it a file descriptor; it is merely a marker used to identify the open file between the client and the server.

$flags should be a bitmask of open flags, whose values can be obtained from Net::SFTP::Constants:

use Net::SFTP::Constants qw( :flags );

$attrs should be a Net::SFTP::Attributes object, specifying the initial attributes for the file $path. If you're opening the file for reading only, $attrs can be left blank, in which case it will be initialized to an empty set of attributes.

$sftp->do_close($handle)

Sends the SSH_FXP_CLOSE command to close either an open file or open directory, identified by $handle (the handle returned from either do_open or do_opendir).

Emits a warning if the CLOSE fails.

Returns the status code for the operation. To turn the status code into a text message, take a look at the fx2txt function in Net::SFTP::Util.

$sftp->do_lstat($path)

$sftp->do_fstat($handle)

$sftp->do_stat($path)

These three methods all perform similar functionality: they run a stat on a remote file and return the results in a Net::SFTP::Attributes object on success.

On failure, all three methods return undef, and emit a warning.

do_lstat sends a SSH_FXP_LSTAT command to obtain file attributes for a named file $path. do_stat sends a SSH_FXP_STAT command, and differs from do_lstat only in that do_stat follows symbolic links on the server, whereas do_lstat does not follow symbolic links.

do_fstat sends a SSH_FXP_FSTAT command to obtain file attributes for an open file handle $handle.

$sftp->do_setstat($path, $attrs)

$sftp->do_fsetstat($handle, $attrs)

These two methods both perform similar functionality: they set the file attributes of a remote file. In both cases $attrs should be a Net::SFTP::Attributes object.

do_setstat sends a SSH_FXP_SETSTAT command to set file attributes for a remote named file $path to $attrs.

do_fsetstat sends a SSH_FXP_FSETSTAT command to set the attributes of an open file handle $handle to $attrs.

Both methods emit a warning if the operation failes, and both return the status code for the operation. To turn the status code into a text message, take a look at the fx2txt function in Net::SFTP::Util.

$sftp->do_opendir($path)

Sends a SSH_FXP_OPENDIR command to open the remote directory $path, and returns an open handle on success. On failure returns undef.

$sftp->do_remove($path)

Sends a SSH_FXP_REMOVE command to remove the remote file $path.

Emits a warning if the operation fails.

Returns the status code for the operation. To turn the status code into a text message, take a look at the fx2txt function in Net::SFTP::Util.

$sftp->do_mkdir($path, $attrs)

Sends a SSH_FXP_MKDIR command to create a remote directory $path whose attributes should be initialized to $attrs, a Net::SFTP::Attributes object.

Emits a warning if the operation fails.

Returns the status code for the operation. To turn the status code into a text message, take a look at the fx2txt function in Net::SFTP::Util.

$sftp->do_rmdir($path)

Sends a SSH_FXP_RMDIR command to remove a remote directory $path.

Emits a warning if the operation fails.

Returns the status code for the operation. To turn the status code into a text message, take a look at the fx2txt function in Net::SFTP::Util.

$sftp->do_realpath($path)

Sends a SSH_FXP_REALPATH command to canonicalise $path to an absolute path. This can be useful for turning paths containing '..' into absolute paths.

Returns the absolute path on success, undef on failure.

$sftp->do_rename($old, $new)

Sends a SSH_FXP_RENAME command to rename $old to $new.

Emits a warning if the operation fails.

Returns the status code for the operation. To turn the status code into a text message, take a look at the fx2txt function in Net::SFTP::Util.

AUTHOR & COPYRIGHTS

Benjamin Trott, ben@rhumba.pair.com

Except where otherwise noted, Net::SFTP is Copyright 2001 Benjamin Trott. All rights reserved. Net::SFTP is free software; you may redistribute it and/or modify it under the same terms as Perl itself.