Skip to content

Instantly share code, notes, and snippets.

@nemunaire
Last active October 12, 2021 20:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nemunaire/e10f9eaf888bdb76895b to your computer and use it in GitHub Desktop.
Save nemunaire/e10f9eaf888bdb76895b to your computer and use it in GitHub Desktop.
Generate FTP commands to send the content of a directory and subdirectories
#!/usr/bin/env perl
#=============================================================================
#
# FILE: tree2ftp.pl
#
# USAGE: ./tree2ftp.pl [options] host path [path ...]
#
# DESCRIPTION: Send a directory to FTP
#
# OPTIONS: -username: Username
# -password: Password
# -createdir: Create a directory before
# REQUIREMENTS: Perl 5.10
# BUGS: none known
# AUTHOR: nemunaire <nemunaire@nemunai.re>
# CREATED: 01/17/2015 12:42:00 PM
#=============================================================================
use v5.10;
use strict;
use warnings;
use autodie;
use File::Basename;
use Getopt::Long;
use Pod::Usage;
### COMMAND-LINE #############################################################
my $RAW = undef;
my $CREATEDIR = undef;
my $USERNAME = "anonymous";
my $PASSWORD = 'anonymous@domain.com';
my $HELP = 0;
my $HOSTNAME = undef;
my @PATH;
GetOptions(
'createdir=s{0,1}' => \$CREATEDIR,
'username=s' => \$USERNAME,
'password=s' => \$PASSWORD,
'help|?' => \$HELP,
) or pod2usage(2);
pod2usage( -exitval => 0, -verbose => 2 ) if $HELP;
$HOSTNAME = shift @ARGV if $#ARGV >= 0;
while ($#ARGV >= 0) {
push @PATH, shift @ARGV;
}
### FUNCTIONS ################################################################
sub tree2ftp {
my $dirpath = shift;
my $ftph = shift // *STDOUT;
opendir(my $dh, $dirpath);
while(readdir $dh) {
next if $_ eq "." || $_ eq "..";
if (-d "$dirpath/$_") {
say $ftph "mkdir $_";
say $ftph "cd $_";
tree2ftp("$dirpath/$_", $ftph);
say $ftph "cd ..";
} else {
say $ftph "put $dirpath/$_ " . basename($_);
}
}
closedir $dh;
}
sub ftpconnect($;$$) {
my $host = shift;
my $username = shift // "anonymous";
my $password = shift // 'anonymous@domain.com';
open my $ftpd, "|-", "ftp -n $host" or die "open: $!";
say $ftpd "quote USER $username";
say $ftpd "quote PASS $password";
return $ftpd;
}
### MAIN ################################################################
pod2usage( -exitval => 1, -verbose => 1 ) if ! defined $HOSTNAME;
my $ftph = ftpconnect($HOSTNAME, $USERNAME, $PASSWORD);
if (defined $CREATEDIR) {
say $ftph "mkdir $CREATEDIR";
say $ftph "cd $CREATEDIR";
}
for my $path (@PATH) {
tree2ftp($path, $ftph);
}
say $ftph "close";
__END__
=head1 NAME
Tree2FTP - Send directories to FTP
=head1 SYNOPSIS
./tree2ftp.pl [OPTIONS] HOST PATH [PATH [...]]
=head1 OPTIONS
=over
=item B<-createdir=%s>
Before copying directories and files, create and change to the given directory.
=item B<-help>
Displays the help.
=item B<-username=%s>
Set the username instead of anonymous.
=item B<-password=%s>
Set the password instead of anonymous@domain.com.
=back
=head1 EXIT STATUS
This script returns 0 when no problem (railsworks are not considered as problem) else it returns 1.
=head1 AUTHOR
Pierre-Olivier Mercier <nemunaire@nemunai.re>
=head1 VERSION
This is B<tree2ftp.pl> version 1.0.
=head1 LICENSE AND COPYRIGHT
B<The GNU GPLv3 License>
Copyright (C) 2014 nemunaire
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 3 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, see <http://www.gnu.org/licenses/>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment