Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

SFTP on Windows

by Limbic~Region (Chancellor)
on Oct 31, 2010 at 23:17 UTC ( #868650=perlquestion: print w/replies, xml ) Need Help??

Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:

All,
Does anyone have a reasonable solution for doing SFTP on Windows? I reached for Net::SFTP and discovered it relied on Net::SSH::Perl which doesn't work on Windows. Next I reached for Net::SFTP::Foreign but had it complain about missing IO::Pty and Expect. Finally, I kludged something together using Net::SSH2.

Some observations: Net::SFTP could be modified to use Net::SSH::W32Perl if it detects it is running on Win32. Also, fixing the two issues Net::SSH::Perl complains about on Win32 is relatively simple (using Win32 to get the login name instead of getpwuid and using $ENV{HOMEPATH} instead of $ENV{HOME}). What I don't know is if anything else would break. Finally, I have seen environments that allow SFTP but not SSH (setting the login shell to /sbin/false for instance). This would make Net::SSH2 not viable but fortunately I didn't run into this hurdle.

Does anyone have a straight forward solution to manipulating SFTP on Windows?

Cheers - L~R

Replies are listed 'Best First'.
Re: SFTP on Windows
by syphilis (Bishop) on Nov 01, 2010 at 01:21 UTC
    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
      syphilis,
      As I indicated, I was able to kludge together a solution using Net::SSH2. There are a couple of reasons why I specifically wanted SFTP. The first is because sometimes that is all that is available (see derby's reply elsewhere in the thread). The second is because SFTP allows you to do things like look around before deciding what to do where you have to know what you want with SCP. I am not saying this is unacceptable but after searching a bit, I was suprised there wasn't a very simple straight forward solution.

      Cheers - L~R

Re: SFTP on Windows
by BrowserUk (Pope) on Oct 31, 2010 at 23:28 UTC

    Is a OSS binary solution acceptable? WinSCP

      BrowserUk,
      I probably should have made it clear that I am not in need of an SFTP client on windows (of those, there are plenty). I am looking at a way of driving SFTP automation through perl. I have not used the client you linked to but at a 2 second glance it doesn't appear to be made for automation. Update: In an additional 2 seconds, I found the "scripting" tab. Perhaps this would be viable - thanks.

      Cheers - L~R

        Update:...

        See also: WinSCP.com.

        My needs have been limited--2 cases in 8 years--but it has everything I've needed.

Re: SFTP on Windows
by saberworks (Curate) on Nov 01, 2010 at 04:42 UTC
    I used Net::SFTP::Foreign for a deployment system. It works from both windows and linux clients. On the windows side, each client has to have plink installed, which is available from putty. You only need Expect if you need password authentication on windows. We didn't since everyone has keys set up.
      saberworks,
      You only need Expect if you need password authentication on windows.

      Exactly. Perhaps I had unrealistic expectations but since Net::SFTP has been around since 2003, I would have hoped that Win32 support was available. If you read the CPAN ratings - it looks stagnant. I am just as suprised that the alternatives require such heavy dependencies. Yes, I know "patches welcome". I am pretty sure I actually made Net::SFTP work on windows by changing 3 lines in Net::SSH::Perl but I am not willing to bet on it.

      Cheers - L~R

Re: SFTP on Windows
by salva (Canon) on Nov 01, 2010 at 10:29 UTC
      You can also run Net::SFTP::Foreign on top of Net::SSH2 using Net::SFTP::Foreign::Backend::Net_SSH2.

      salva, what are the advantages of doing that - as opposed to simply using Net::SSH2 ?

      I'm thinking that if it's using Net::SSH2 then Net::SFTP::Foreign::Backend::Net_SSH2 is not going to provide any performance boost ... therefore, maybe it provides some advantages when it comes to writing the code ?
      Does it, for example, simplify the coding of an sftp_put() ?

      Cheers,
      Rob
        I'm thinking that if it's using Net::SSH2 then Net::SFTP::Foreign::Backend::Net_SSH2 is not going to provide any performance boost
        Actually it is!

        Net::SSH2 support for SFTP (Net::SSH2::SFTP) is very basic and implementing efficient file transfer operations on top of it is far from trivial.

        Besides that, Net::SFTP::Foreign provides a bunch of high level methods such as find, glob, mput, rput, etc.

      salva,
      The client I had kludged a working solution for asked for yet another feature. Instead of shoehorning it in, I tried your suggestion of Net::SFTP::Foreign::Backend::Net_SSH2. I will now be using this as my SFTP solution for Windows boxes in the future - thank you!

      Cheers - L~R

Re: SFTP on Windows
by Plankton (Vicar) on Nov 01, 2010 at 00:54 UTC
    You probably have already thought of this but just in case ... cygwin ?
      Plankton,
      Yes, while it would work - it falls in the category of what I perceive to be an unecessary hurdle.

      Cheers - L~R

Re: SFTP on Windows
by jjap (Monk) on Nov 01, 2010 at 03:09 UTC
    I probably don't know enough to even figure how far off the tracks I am
    but...
    Wouldn't it be feasible to call PSFTP (PuTTY)?
      jjap,
      The SFTP client (psftp.exe) that comes with PuTTY doesn't lend itself to automation.

      Cheers - L~R

        There are the -b file, -bc, and -be option on my psftp client. If you are looking to just drive psftp with a batch script, perhaps the -b file would suffice.

        See psftp -h and the putty docs for details.

        --MidLifeXis

Re: SFTP on Windows
by aquarium (Curate) on Nov 01, 2010 at 01:52 UTC
    isn't sftp just ftp after establishing an ssh tunnel?..in which case you should be able to do likewise. i think the sftp modules just provide a bit of convenience.
    the hardest line to type correctly is: stty erase ^H

      No, it's not. SFTP is a different protocol as is FTPS. FTP over SSH is normally called Secure FTP but there are a ton of products with similar names so the waters are really muddy. I have to explain these differences on a monthly basis to new customers (we support SFTP only). They normally complain bitterly *after* they've set up FTPS or Secure FTP. I guess it's an example of the second toughest problem in software development - naming things.

      -derby

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://868650]
Approved by planetscape
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2021-04-20 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?