#!/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!
|