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
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.