#!/usr/bin/perl -w
#---------#---------#---------#---------#---------#---------#---------#---------
#
# Date:         18 Oct 00
# Title:        copy a file from another host using Net::FTP
# Creator:      idc@planetlarg.net
# Description:
#   the script takes a file name with its path.
#   it logs into all the isp01 ldap hosts and gets the file.
#   to prevent each file from overwriting the last, each file
#   has the hostname appended to it.
# Modifications:
#
#---------#---------#---------#---------#---------#---------#---------#---------

# hosts and accounts
   my %hosts      = (
      "ldp01"  => "10.216.4.28",
      "ldp02"  => "10.216.4.53",
      "lds01"  => "10.216.4.34",
      "lds02"  => "10.216.4.59",
      "lds03"  => "10.216.4.35",
      "lds04"  => "10.216.4.60"
   );                               # the magnificent six

   my $username01 = "isp01";       # account is common to all isp01 hosts
   my $password01 = "passw0rd";

# why roll your own sockets when you can use a rolling machine?
   use Net::FTP;
   my $ftp;

# status message indent system
   # prints details of its progress to STDOUT
   my $indent = '   ';
   my $spaces = 0;

# command line options (using a perl standard library)
   use Getopt::Long;
   # options (values are inserted into the variables $opt_...)
   # debug -  the ftp session transcript is printed to STDERR
   # file  -  the file to get
   $opt_debug = undef;
   GetOptions( "debug!", "file=s" );
   unless (defined $opt_file) {
      print "usage: \n   $0 [--debug] --file <path/and/file.name> \n\n";
      exit 1;
   }

# files and directories (another perl standard library)
   use File::Basename;
   my ($remote_file, $suffix);                   # file to transfer
   ($remote_file, $remote_dir, $suffix) = fileparse( "$opt_file", '');
#   my $local_dest = '/home/isp01/bin/ftp';

#---------#---------#---------#---------#---------#---------#---------#---------
   $spaces++;
   foreach $host ( keys %hosts ) {

      $local_file = "$remote_file.$host";

      # status message
      print $indent x $spaces . "logging into $hosts{$host} ... \n";

      $ftp = Net::FTP->new(
               "$hosts{$host}",
               Debug => $opt_debug ? 1 : 0
      );
      $ftp->login("$username01", "$password01");


      # status message
      print $indent x $spaces . "copying from file $remote_file to file $local_file ... ";

#      chdir "$local_dest" or die "can't cd to $local_dest: $!";
      $ftp->cwd("$remote_dir");

# get: copy files from remote to local host
#      $ftp->type('binary'); # can be ascii, binary, ebcdic or byte
      $copied = $ftp->get("$remote_file", "$local_file" );
#      $ftp->type('ascii'); # can be ascii, binary, ebcdic or byte
      if ( $copied ) {
         print " \n";
      } else {
         print "failed! \n";
      }

# clean up
      $ftp->quit;
   }
   $spaces--;
   print "done. \n";


