Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #SeedBox FTP Download Script
- #By Juvenal
- #Uploads files matching the extension filter from the working directory(or specified)
- #to the ftp server specified with the same directory structure
- use File::Basename;
- use File::Find;
- use Net::FTP;
- use Switch;
- use Cwd;
- #------------- DO NOT EDIT ABOVE THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING ------------
- #User Editable Vars
- $filter = '.*';
- $rootdir = "/home/slice9/Downloads";
- $sourcedir = cwd();
- $ftp_usr = "user";
- $ftp_pass = "pass";
- $ftp_port = "21";
- $ftp_host = "remote.host";
- $ftp_root = "";
- #------------- DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING ------------
- #Variables
- @filelist;
- @dirlist;
- $debug = 1;
- $testing = 0;
- $ftp;
- #Get Switches
- if ($#ARGV != -1)
- {
- foreach $argnum (0 .. $#ARGV)
- {
- my $arg = $ARGV[$argnum];
- my $narg = $ARGV[$argnum + 1];
- switch (lc $arg)
- {
- case "-s" { $sourcedir = $narg }
- case "-h" { $ftp_host = $narg }
- case "-t" { $ftp_port = $narg }
- case "-u" { $ftp_usr = $narg }
- case "-r" { $ftp_root = $narg }
- case "-p" { $ftp_pass = $narg }
- case "-f" { $filter = $narg }
- case "-l" { $rootdir = $narg }
- case "-help" { Help() }
- }
- }
- }
- #List Directory
- find({wanted => \&HandleFile, no_chdir => 1}, $sourcedir);
- RemoveEmptyDirs();
- if ($debug)
- {
- print "Out:\n";
- foreach $ent (@filelist)
- {
- print "$ent\n";
- }
- foreach $ent (@dirlist)
- {
- print "$ent\n";
- }
- }
- #Open FTP
- $ftp = Net::FTP->new($ftp_host, Port => $ftp_port)
- or die "Cannot Connect to FTP Server: $@";
- $ftp->login($ftp_usr, $ftp_pass)
- or die "Cannot Login to FTP Server: ", $ftp->message;
- $ftp->cwd($ftp_root)
- or die "Cannot change to FTP Root: ", $ftp->message;
- $ftp->binary();
- foreach $ent (@dirlist)
- {
- print "Making Directory: $ent...\n";
- if ($testing eq 0)
- {
- $ftp->mkdir($ftp_root.$ent, 1)
- or die "Cannot Make Directory $ent: ", $ftp->message;
- }
- print "DONE\n"
- }
- foreach $ent (@filelist)
- {
- $fsize = -s $rootdir.$ent;
- $fsz = $fsize / 1024;
- $fsz = sprintf("%.2f", $fsz);
- print "Sending File(".$fsz."K): $ent...\n";
- SetHash($fsize);
- if ($testing eq 0)
- {
- $ftp->put($rootdir.$ent, $ftp_root.$ent)
- or die "Cannot Put File $ent: ", $ftp->message;
- }
- print "DONE\n";
- }
- $ftp->quit();
- #Exit
- exit(0);
- #Functions
- sub RemoveEmptyDirs()
- {
- my @tmpdlist = @dirlist;
- my @tmplist;
- foreach $ent (@filelist)
- {
- foreach $dir (@tmpdlist)
- {
- if ($ent =~ /^$dir.*/)
- {
- if ($dir ne "") {
- push(@tmplist, $dir);
- }
- my( $index )= grep { $tmpdlist[$_] eq $dir } 0..$#tmpdlist;
- delete $tmpdlist[$index];
- }
- }
- }
- @dirlist = @tmplist;
- }
- sub HandleFile()
- {
- my $filename = $_;
- if (-f $filename)
- {
- $filename =~ s/$rootdir//;
- if ($filename =~ /.*\.($filter)/)
- {
- push(@filelist, $filename);
- }
- }
- if (-d $filename)
- {
- $filename =~ s/$rootdir//;
- push(@dirlist, $filename);
- }
- }
- sub SetHash($)
- {
- my $fsize = $_[0];
- my $hashval = 1024000;
- $hashval = $fsize / 50;
- $ftp->hash(STDOUT,$hashval);
- }
- sub Help()
- {
- print "Usage: DownloadFiles \[Switches\]\n";
- print "EX: DownloadFiles -h ftp.my.com -u JoeSmith -p 12345 -r /Download/Seedbox/\n\n";
- print "Switches:\n";
- print "-s Source Directory, Where to read files from\n";
- print "-f Extension Regex Filter (Default .*) Separate with | Ex:\n";
- print " rar|bin|txt|d.*\n";
- print "-l Local Root Directory (Replaced by FTP Root in file paths)\n";
- print "-h FTP Host\n";
- print "-t FTP Port\n";
- print "-u FTP User\n";
- print "-p FTP Password\n";
- print "-r FTP Root Directory\n";
- print "\n\n";
- print "All Switches can have defaults set by editing the script\n";
- print "Other than Source Directory, which defaults to the Working Directory\n";
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement