Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Windoze:SFtp into a scalar

by NateTut (Deacon)
on Dec 02, 2010 at 22:22 UTC ( #875026=perlquestion: print w/ replies, xml ) Need Help??
NateTut has asked for the wisdom of the Perl Monks concerning the following question:

All I want to do is read the contents of a file using sftp into a scalar. I've spent all afternoon going down dead end after dead end. Help!

Solved!
First I was using the wrong repository. Once I got a 5.10 repository the install of Net::SSH2 went much better. Next I had to install Net::SFTP::Foreign::Backend::Net_SSH2 no ppd I could find, but it had no xs to compile, so the manual install method did the trick.

Thanks to all the monks for their help!

Comment on Windoze:SFtp into a scalar
Download Code
Re: Windoze:SFtp into a scalar
by ww (Bishop) on Dec 02, 2010 at 22:32 UTC
Re: Windoze:SFtp into a scalar
by aquarium (Curate) on Dec 02, 2010 at 23:22 UTC
    you'll need to be a bit more specific as to what you've tried and how it failed. i know this sounds a bit silly from your point of view, because that's what already failed. however, we need to get a bit more understanding on what you're trying to achieve, beyond the very brief description.
    btw from all that i know, and i haven't used sftp perl module myself, i expect that the module will allow you to transfer files from a secure ftp server..and once transferred you can open and read them etc. for all i know the module just may let you read into scalar instead.
    in any case, provide more information to get better answers. have you tested transferring the file(s) from the server to client just using a sftp client? how are you using sftp module to slurp remote file into scalar?
    the hardest line to type correctly is: stty erase ^H
Re: Windoze:SFtp into a scalar
by syphilis (Canon) on Dec 03, 2010 at 00:51 UTC
    This works for me on windows:
    use warnings; use strict; use Net::SFTP::Foreign; my $host = 'xxx.xxx.xxx.xxx'; my $user = 'user'; my $pass = 'pass'; my $file = 'whatever'; my $contents; my $sftp = Net::SFTP::Foreign->new ($host, backend => 'Net_SSH2', username => $user, password => $pass); $sftp->error and die "Unable to establish SFTP connection: " . $sftp->error; $sftp->get($file, "foo.txt") or die "get failed: " . $sftp->error; open RD, '<', "foo.txt" or die $!; while (<RD>) {$contents .= $_} close RD or die "$!"; print $contents; unlink "foo.txt";
    It needs both Net::SSH2 and Net::SFTP::Foreign.

    Cheers,
    Rob

    UPDATE: Or, to get it (more) directly into a scalar, we can do this (or something similar):
    use warnings; use strict; use Net::SFTP::Foreign; my $host = 'xxx.xxx.xxx.xxx'; my $user = 'user'; my $pass = 'pass'; my $file = 'whatever'; my $contents; my $sftp = Net::SFTP::Foreign->new ($host, backend => 'Net_SSH2', username => $user, password => $pass); $sftp->error and die "Unable to establish SFTP connection: " . $sftp->error; close STDOUT; open STDOUT, '>', \$contents or die "Can't open STDOUT: $!"; $sftp->get($file, \*STDOUT) or die "get failed: " . $sftp->error; warn $contents;
      Net::SFTP::Foreign already provides a get_content method:
      my $content = $sftp->get_content($file) // die "unable to retrieve file $file: " . $sftp->error;
        Net::SFTP::Foreign already provides a get_content method

        I s'pose I could always claim that's what I meant by "something similar" ... though it would, in fact, be more appropriately described as "something better" ;-)

        Cheers,
        Rob
Re: Windoze:SFtp into a scalar
by NateTut (Deacon) on Dec 03, 2010 at 15:39 UTC
    Thanks for the great suggestions & sample code. I'm hung up on trying to get Net::SSH2 installed. I tried

    C:\Src\pl\ParseAWSEvents>ppm install http://theoryx5.uwinnipeg.ca/ppms +/Net-SSH2.ppd ppm install failed: The PPD does not provide code to install for this +platform

    I'm running ActiveState 5.10 BTW. I couldn't find Net::SSH2 in any of the repositories I've configured in ppm.
      Actually, Net::SSH2 is not absolutely required. As far as you use public key authentication, the default Net::SFTP::Foreign backend will happily use plink or openssh to connect to the remote server.

      Though, in Windows, if you want to authenticate using a private password, things become harder because the module uses Expect to implement that feature but that module does not work under ActiveState (or Strawberry) Perl. An alternative is to pass the password to plink in the command line using the -pw switch but anyone able to log to the machine (or to run arbitrary programs there) will be able to see the password so I usually disrecommend this method. See the module FAQ.

      ppm install http://theoryx5.uwinnipeg.ca/ppms/Net-SSH2.ppd

      That's the wrong command for perl 5.10. Instead try:

      ppm install http://cpan.uwinnipeg.ca/PPMPackages/10xx/Net-SSH2.ppd

      Cheers,
      Rob

      Update:
      Or, for all versions of Win32 perl from perl-5.6 thru to 5.12 (including x64 builds of 5.12) the following should now do the trick:

      ppm install http://www.sisyphusion.tk/ppm/Net-SSH2.ppd

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-12-18 04:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls