Perl SFTP File Listing

The Net::SFTP::Foreign module in CPAN is a robust SFTP library which allows you to handle many types of operations.    

Below is a code snippet that will populate an array with all the filenames living under a base directory.


#!/usr/bin/perl
use strict;
use warnings;

use Fcntl qw(S_ISREG);
use Net::SFTP::Foreign;

my $host = 'sftp.example.com';
my $port = 22;
my $user = 'username';
my $password = 'xxxxx';

my $path = '/tmp';

my $sftp = Net::SFTP::Foreign->new($host,port => $port, user => $user, password => $password, stderr_discard => 1);

my @list = $sftp->find($path, names_only => 1, wanted => sub { S_ISREG($_[1]->{a}->perm)});