Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Using Net::SFTP

by thanos1983 (Parson)
on Feb 22, 2018 at 12:02 UTC ( [id://1209738]=note: print w/replies, xml ) Need Help??


in reply to Using Net::SFTP

Hello slugger415,

Fellow Monks have provided you with multiple solutions. Just to add one more :):

#!/usr/bin/perl use strict; use warnings; use Net::SFTP; my $host = '127.0.0.1'; my %args = ( user => 'tinyos', ssh_args => [ port => "22" ] ); my $sftp = Net::SFTP->new($host, %args); my $ls = $sftp->ls('/home/user/Monks'); print "$_->{filename}\n" for (@$ls); __END__ $ perl test.pl benchmark.pl~ Foo sample.pl test.log out.txt sample.pl~ .. .. ..

Minor note to make here, I am not using password I use ssh keys, I think you should do the same is far far more safe.

Having said all that why not use Net::SFTP::Foreign? Why I should you use this module? Simply read here Net::SFTP::Foreign Vs. Net::SFTP Vs. Net::SSH2::SFTP.

Sample of code just as your example:

#!usr/bin/perl use strict; use warnings; use Net::SFTP::Foreign; my %args = ( host => "127.0.0.1", user => "user", port => "22", # psw => "psw", # uncomment if you are using passwords key_path => "/home/user/.ssh/id_rsa" ); # comment if you are +using passwords my $sftp = Net::SFTP::Foreign->new(%args); $sftp->die_on_error("Unable to establish SFTP connection"); my $ls = $sftp->ls('/home/user/Monks') or die "unable to retrieve directory: ".$sftp->error; print "$_->{filename}\n" for (@$ls); __END__ $ perl test.pl benchmark.pl~ Foo sample.pl test.log out.txt sample.pl~ .. .. ..

Both examples above are based on my local configurations, so simply update them based on your configurations.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found