1Net::SFTP(3) User Contributed Perl Documentation Net::SFTP(3)
2
3
4
6 Net::SFTP - Secure File Transfer Protocol client
7
9 use Net::SFTP;
10 my $sftp = Net::SFTP->new($host);
11 $sftp->get("foo", "bar");
12 $sftp->put("bar", "baz");
13
15 Net::SFTP is a pure-Perl implementation of the Secure File Transfer
16 Protocol (SFTP) - file transfer built on top of the SSH2 protocol.
17 Net::SFTP uses Net::SSH::Perl to build a secure, encrypted tunnel
18 through which files can be transferred and managed. It provides a
19 subset of the commands listed in the SSH File Transfer Protocol IETF
20 draft, which can be found at
21 http://www.openssh.com/txt/draft-ietf-secsh-filexfer-00.txt.
22
23 SFTP stands for Secure File Transfer Protocol and is a method of
24 transferring files between machines over a secure, encrypted connection
25 (as opposed to regular FTP, which functions over an insecure
26 connection). The security in SFTP comes through its integration with
27 SSH, which provides an encrypted transport layer over which the SFTP
28 commands are executed, and over which files can be transferred. The
29 SFTP protocol defines a client and a server; only the client, not the
30 server, is implemented in Net::SFTP.
31
32 Because it is built upon SSH, SFTP inherits all of the built-in
33 functionality provided by Net::SSH::Perl: encrypted communications
34 between client and server, multiple supported authentication methods
35 (eg. password, public key, etc.).
36
38 Net::SFTP->new($host, %args)
39 Opens a new SFTP connection with a remote host $host, and returns a
40 Net::SFTP object representing that open connection.
41
42 %args can contain:
43
44 · user
45
46 The username to use to log in to the remote server. This should be
47 your SSH login, and can be empty, in which case the username is
48 drawn from the user executing the process.
49
50 See the login method in Net::SSH::Perl for more details.
51
52 · password
53
54 The password to use to log in to the remote server. This should be
55 your SSH password, if you use password authentication in SSH; if
56 you use public key authentication, this argument is unused.
57
58 See the login method in Net::SSH::Perl for more details.
59
60 · debug
61
62 If set to a true value, debugging messages will be printed out for
63 both the SSH and SFTP protocols. This automatically turns on the
64 debug parameter in Net::SSH::Perl.
65
66 The default is false.
67
68 · warn
69
70 If given a sub ref, the sub is called with $self and any warning
71 message; if set to false, warnings are supressed; otherwise they
72 are output with 'warn' (default).
73
74 · ssh_args
75
76 Specifies a reference to a list or hash of named arguments that
77 should be given to the constructor of the Net::SSH::Perl object
78 underlying the Net::SFTP connection.
79
80 For example, you could use this to set up your authentication
81 identity files, to set a specific cipher for encryption, etc., e.g.
82 "ssh_args => [ cipher => 'arcfour' ]".
83
84 See the new method in Net::SSH::Perl for more details.
85
86 $sftp->status
87 Returns the last remote SFTP status value. Only useful after one of
88 the following methods has failed. Returns SSH2_FX_OK if there is no
89 remote error (e.g. local file not found). In list context, returns a
90 list of (status code, status text from "fx2txt").
91
92 If a low-level protocol error or unexpected local error occurs, we die
93 with an error message.
94
95 $sftp->get($remote [, $local [, \&callback ] ])
96 Downloads a file $remote from the remote host. If $local is specified,
97 it is opened/created, and the contents of the remote file $remote are
98 written to $local. In addition, its filesystem attributes (atime,
99 mtime, permissions, etc.) will be set to those of the remote file.
100
101 If get is called in a non-void context, returns the contents of $remote
102 (as well as writing them to $local, if $local is provided. Undef is
103 returned on failure.
104
105 $local is optional. If not provided, the contents of the remote file
106 $remote will be either discarded, if get is called in void context, or
107 returned from get if called in a non-void context. Presumably, in the
108 former case, you will use the callback function \&callback to "do
109 something" with the contents of $remote.
110
111 If \&callback is specified, it should be a reference to a subroutine.
112 The subroutine will be executed at each iteration of the read loop
113 (files are generally read in 8192-byte blocks, although this depends on
114 the server implementation). The callback function will receive as
115 arguments: a Net::SFTP object with an open SFTP connection; the data
116 read from the SFTP server; the offset from the beginning of the file
117 (in bytes); and the total size of the file (in bytes). You can use this
118 mechanism to provide status messages, download progress meters, etc.:
119
120 sub callback {
121 my($sftp, $data, $offset, $size) = @_;
122 print "Read $offset / $size bytes\r";
123 }
124
125 $sftp->put($local, $remote [, \&callback ])
126 Uploads a file $local from the local host to the remote host, and saves
127 it as $remote.
128
129 If \&callback is specified, it should be a reference to a subroutine.
130 The subroutine will be executed at each iteration of the write loop,
131 directly after the data has been read from the local file. The callback
132 function will receive as arguments: a Net::SFTP object with an open
133 SFTP connection; the data read from $local, generally in 8192-byte
134 chunks;; the offset from the beginning of the file (in bytes); and the
135 total size of the file (in bytes). You can use this mechanism to
136 provide status messages, upload progress meters, etc.:
137
138 sub callback {
139 my($sftp, $data, $offset, $size) = @_;
140 print "Wrote $offset / $size bytes\r";
141 }
142
143 Returns true on success, undef on error.
144
145 $sftp->ls($remote [, $subref ])
146 Fetches a directory listing of $remote.
147
148 If $subref is specified, for each entry in the directory, $subref will
149 be called and given a reference to a hash with three keys: filename,
150 the name of the entry in the directory listing; longname, an entry in a
151 "long" listing like "ls -l"; and a, a Net::SFTP::Attributes object,
152 which contains the file attributes of the entry (atime, mtime,
153 permissions, etc.).
154
155 If $subref is not specified, returns a list of directory entries, each
156 of which is a reference to a hash as described in the previous
157 paragraph.
158
160 Net::SFTP supports all of the commands listed in the SFTP version 3
161 protocol specification. Each command is available for execution as a
162 separate method, with a few exceptions: SSH_FXP_INIT, SSH_FXP_VERSION,
163 and SSH_FXP_READDIR.
164
165 These are the available command methods:
166
167 $sftp->do_open($path, $flags [, $attrs ])
168 Sends the SSH_FXP_OPEN command to open a remote file $path, and returns
169 an open handle on success. On failure returns undef. The "open handle"
170 is not a Perl filehandle, nor is it a file descriptor; it is merely a
171 marker used to identify the open file between the client and the
172 server.
173
174 $flags should be a bitmask of open flags, whose values can be obtained
175 from Net::SFTP::Constants:
176
177 use Net::SFTP::Constants qw( :flags );
178
179 $attrs should be a Net::SFTP::Attributes object, specifying the initial
180 attributes for the file $path. If you're opening the file for reading
181 only, $attrs can be left blank, in which case it will be initialized to
182 an empty set of attributes.
183
184 $sftp->do_read($handle, $offset, $copy_size)
185 Sends the SSH_FXP_READ command to read from an open file handle
186 $handle, starting at $offset, and reading at most $copy_size bytes.
187
188 Returns a two-element list consisting of the data read from the SFTP
189 server in the first slot, and the status code (if any) in the second.
190 In the case of a successful read, the status code will be undef, and
191 the data will be defined and true. In the case of EOF, the status code
192 will be SSH2_FX_EOF, and the data will be undef. And in the case of an
193 error in the read, a warning will be emitted, the status code will
194 contain the error code, and the data will be undef.
195
196 $sftp->do_write($handle, $offset, $data)
197 Sends the SSH_FXP_WRITE command to write to an open file handle
198 $handle, starting at $offset, and where the data to be written is in
199 $data.
200
201 Returns the status code. On a successful write, the status code will be
202 equal to SSH2_FX_OK; in the case of an unsuccessful write, a warning
203 will be emitted, and the status code will contain the error returned
204 from the server.
205
206 $sftp->do_close($handle)
207 Sends the SSH_FXP_CLOSE command to close either an open file or open
208 directory, identified by $handle (the handle returned from either
209 do_open or do_opendir).
210
211 Emits a warning if the CLOSE fails.
212
213 Returns the status code for the operation. To turn the status code into
214 a text message, take a look at the "fx2txt" function in
215 Net::SFTP::Util.
216
217 $sftp->do_lstat($path)
218 $sftp->do_fstat($handle)
219 $sftp->do_stat($path)
220 These three methods all perform similar functionality: they run a stat
221 on a remote file and return the results in a Net::SFTP::Attributes
222 object on success.
223
224 On failure, all three methods return undef, and emit a warning.
225
226 do_lstat sends a SSH_FXP_LSTAT command to obtain file attributes for a
227 named file $path. do_stat sends a SSH_FXP_STAT command, and differs
228 from do_lstat only in that do_stat follows symbolic links on the
229 server, whereas do_lstat does not follow symbolic links.
230
231 do_fstat sends a SSH_FXP_FSTAT command to obtain file attributes for an
232 open file handle $handle.
233
234 $sftp->do_setstat($path, $attrs)
235 $sftp->do_fsetstat($handle, $attrs)
236 These two methods both perform similar functionality: they set the file
237 attributes of a remote file. In both cases $attrs should be a
238 Net::SFTP::Attributes object.
239
240 do_setstat sends a SSH_FXP_SETSTAT command to set file attributes for a
241 remote named file $path to $attrs.
242
243 do_fsetstat sends a SSH_FXP_FSETSTAT command to set the attributes of
244 an open file handle $handle to $attrs.
245
246 Both methods emit a warning if the operation failes, and both return
247 the status code for the operation. To turn the status code into a text
248 message, take a look at the "fx2txt" function in Net::SFTP::Util.
249
250 $sftp->do_opendir($path)
251 Sends a SSH_FXP_OPENDIR command to open the remote directory $path, and
252 returns an open handle on success. On failure returns undef.
253
254 $sftp->do_remove($path)
255 Sends a SSH_FXP_REMOVE command to remove the remote file $path.
256
257 Emits a warning if the operation fails.
258
259 Returns the status code for the operation. To turn the status code into
260 a text message, take a look at the "fx2txt" function in
261 Net::SFTP::Util.
262
263 $sftp->do_mkdir($path, $attrs)
264 Sends a SSH_FXP_MKDIR command to create a remote directory $path whose
265 attributes should be initialized to $attrs, a Net::SFTP::Attributes
266 object.
267
268 Emits a warning if the operation fails.
269
270 Returns the status code for the operation. To turn the status code into
271 a text message, take a look at the "fx2txt" function in
272 Net::SFTP::Util.
273
274 $sftp->do_rmdir($path)
275 Sends a SSH_FXP_RMDIR command to remove a remote directory $path.
276
277 Emits a warning if the operation fails.
278
279 Returns the status code for the operation. To turn the status code into
280 a text message, take a look at the "fx2txt" function in
281 Net::SFTP::Util.
282
283 $sftp->do_realpath($path)
284 Sends a SSH_FXP_REALPATH command to canonicalise $path to an absolute
285 path. This can be useful for turning paths containing '..' into
286 absolute paths.
287
288 Returns the absolute path on success, undef on failure.
289
290 $sftp->do_rename($old, $new)
291 Sends a SSH_FXP_RENAME command to rename $old to $new.
292
293 Emits a warning if the operation fails.
294
295 Returns the status code for the operation. To turn the status code into
296 a text message, take a look at the "fx2txt" function in
297 Net::SFTP::Util.
298
300 For samples/tutorials, take a look at the scripts in eg/ in the
301 distribution directory.
302
303 There is a mailing list for development discussion and usage questions.
304 Posting is limited to subscribers only. You can sign up at
305 http://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users
306
307 Please report all bugs via rt.cpan.org at
308 https://rt.cpan.org/NoAuth/ReportBug.html?Queue=net%3A%3Asftp
309
311 Current maintainer is David Robins, dbrobins@cpan.org.
312
313 Previous maintainer was Dave Rolsky, autarch@urth.org.
314
315 Originally written by Benjamin Trott.
316
318 Copyright (c) 2001-2003 Benjamin Trott, Copyright (c) 2003-2004 David
319 Rolsky. Copyright (c) David Robins. All rights reserved. This
320 program is free software; you can redistribute it and/or modify it
321 under the same terms as Perl itself.
322
323 The full text of the license can be found in the LICENSE file included
324 with this module.
325
326
327
328perl v5.12.0 2005-10-05 Net::SFTP(3)