20050605 copy of a copy

From s5h.net

Jump to: navigation, search

Should the need ever arise, heres a smart way to download everything off your ftp server (perl required).

#!/usr/bin/perl -w

use strict;
use warnings;
use Net::FTP;
use Cwd;

# ftpcopy.pl, crude website search
# Copyright (C) 2006, ed neville
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# purpose:
#  to download a ftp directory and all subdirectories
#
# 20060105 ewn v0.1 inital program version.

my $ftp = new Net::FTP( "ftp.servername.com", Passive => 1 );
$ftp->login( "loginname", "password" );

my $localpath = getcwd;
my @remotepaths;

print $localpath;

sub listing
{
  my $path = shift;  
  print "Path: $path\n";
  if( $path eq "/" )
  {
    $path = "";
  }
  $ftp->cwd( "/$path" );
  my @listing = $ftp->dir();
  foreach my $file ( @listing )
  {
    $file =~ /^(.{10})(.*)([A-Za-z]{3}\s+[0-9]{1,2}\s+[0-9:]{4,5})\s+(.*)/;
    my $filename = $4;
    if( $file =~ /^d/ )
    {
      print "Dir $filename\n";
      print "$localpath/$path$filename\n";
      if( ! -d "$localpath/$path$filename" )
      {
        mkdir "$localpath/$path$filename";
      }
      listing( "$path$filename/" );

    }
    else
    {
      print "About to get $localpath/$path$filename\n";
      $ftp->get( $filename, "$localpath/$path$filename" );
    }
  }
}

push( @remotepaths, "/" );
while( 1 )
{
  my $seekdir = pop( @remotepaths );
  listing( $seekdir );
  if( scalar( @remotepaths ) == 0 )
  {
    last;
  }

}
$ftp->quit;
Personal tools
Google AdSense