I use Net::SSH2 to handle both sftp and scp.
As regards coding, it's much simpler to do the transfer via scp:
$ret = $ssh2->scp_put($local, $remote);
In order to sftp, one first has to write the code that's going to open the file on the remote server, read the local file into a buffer, then transmit the local file (one buffer at a time) to the remote server.
I would expect that if the server you're connecting to can handle sftp, then it can also handle scp - and I therefore recommend using scp.
However, if you've already written the sub that handles the sftp, then there's no reason to not keep using it.
I've found it necessary (over slow connections) to patch SSH2.pm as follows, in order for scp to work correctly. (This patch is already included in the ppm packages from the uwinnipeg repo.):
--- SSH2.pm_orig Mon Aug 23 20:06:32 2010
+++ SSH2.pm Mon Aug 23 20:36:30 2010
@@ -373,8 +373,18 @@
$self->error(0, "want $block, have $count"), return
unless $count == $block;
die 'sysread mismatch' unless length $buf == $count;
- $self->error(0, "error writing $count bytes to channel"), retur
+n
- unless $chan->write($buf) == $count;
+ my $wrote = 0;
+ while ($wrote >= 0 && $wrote < $count) {
+ my $wr = $chan->write($buf);
+ last if $wr < 0;
+ $wrote += $wr;
+ $buf = substr $buf, $wr;
+ }
+ unless($wrote == $count) {
+ my @error = $self->error();
+ warn "Error writing $count bytes to channel: @error\n";
+ return;
+ }
}
# send/receive SCP acknowledgement
See the
discussion about that bug for further details.
Cheers,
Rob
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.