#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use Getopt::Long; use Pod::Usage; use Net::SFTP::Foreign::Compat; use File::Find; use File::Basename; use vars qw( $opt_verbose $opt_path $opt_type @queue ); GetOptions( "path=s" => \$opt_path, "type|t=s" => \$opt_type, "verbose|v" => \$opt_verbose, ); my $remoteh = get_uploader( debug => $opt_verbose, host => 'remote_host', port => 5005, user => 'username', priv_key => '/root/content_upload/.ssh/private.ppk', ); my $target_root = 'remote05-0/artwork' find( { wanted => \&found, follow => 1 }, $opt_path ); my $count = 0; my $to; foreach my $file ( @queue ){ $count++; print $count, "\t", $file, "\n"; # Get the filename and use only the first 3 digits to get a subdirectory name my $basename = basename( $file, '.jpg' ); $basename =~ m/(\d{3})/; my $remote_dir = $1; # Build up the full path and name of the file for the remote system $to = join '/', $target_root, $remote_dir, $basename . '.jpg'; # NOTE: The do_opendir() and do_mkdir() correspond to the line 67 & 68 errors in the output # See if the subdir exists on the remote system and, if not, create it my $check_path = $remoteh->do_opendir( $target_root . '/' . $remote_dir ); if( ! $check_path ){ $remoteh->do_mkdir( $target_root . '/' . $remote_dir, Net::SFTP::Foreign::Attributes->new() ); } # NOTE: The put() below corresponds to the Line 77 errors in the output # Copy it up my $copy_status = $remoteh->put( $file, $to ); } sub found{ # We only want jpegs starting with a digit return unless m/\.jpg$/i; return unless m/^\d/; # put the entire path/filename in an array push @queue, $File::Find::name; } sub get_uploader{ my ( %args ) = @_; my %ssh_args = ( identity_file => $args{'priv_key'}, ); my %sftp_args = ( user => $args{'user'}, port => $args{'port'}, debug => $args{'debug'}, ssh_args => \%ssh_args, ); return Net::SFTP::Foreign::Compat->new( $args{'host'}, %sftp_args ); }