Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Windoze:SFtp into a scalar

by syphilis (Archbishop)
on Dec 03, 2010 at 00:51 UTC ( [id://875046]=note: print w/replies, xml ) Need Help??


in reply to Windoze:SFtp into a scalar

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;

Replies are listed 'Best First'.
Re^2: Windoze:SFtp into a scalar
by salva (Canon) on Dec 03, 2010 at 10:46 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://875046]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-04-23 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found