1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #!/usr/bin/perl
use warnings;
use strict;
use Carp;
use Net::FTP;
use Net::FTP::File;
print "Connecting to FTP ... \n";
# create a new instance of the FTP connection
my $ftp = Net::FTP->new( "ftp.ncbi.nlm.nih.gov", Debug => 0 ) or die("Cannot connect $!");
# login to the server
$ftp->login( "anonymous", "anonymous" ) or die("Login failed $!");
print "Logged in ... \n";
my @data= ();
# Limite profondeur
my $limite = 4;
print <<"RESUME";
===============================================================================
Parcours repertoire distant via FTP avec un niveau de profondeur de $limite
===============================================================================
RESUME
getRecursiveDirListing( $ftp, "/genbank/genomes/Eukaryotes", \@data, 0, $limite );
foreach ( @data ) {
print "$_\n";
}
# close the FTP connection
$ftp->quit();
# Parcours recursif FTP avec profondeur définie
sub getRecursiveDirListing {
my ( $ftp, $repertoire_distant, $ref_data, $profondeur, $limite ) = @_;
push ( @{$ref_data}, $repertoire_distant);
print "$repertoire_distant\n";
$ftp->cwd($repertoire_distant) or die "CWD impossible : $repertoire_distant\n", $ftp->message;
# parse and loop through the directory listing
foreach my $filerep ( $ftp->ls() ) {
my $nom_complet = "$repertoire_distant/$filerep";
if ( $ftp->isdir($filerep) ) {
if ( $profondeur < $limite ) {
print "Profondeur : $profondeur OK\n";
getRecursiveDirListing( $ftp, $nom_complet, $ref_data, $profondeur+1, $limite);
}
else {
print "Profondeur depassee : $profondeur - listing interdit\n";
}
}
else {
push ( @{$ref_data}, $nom_complet);
print "$nom_complet\n";
}
}
$ftp->cwd('..') or die "Retour arrier impossible\n", $ftp->message;
return;
} |
Partager