Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #SeedBox FTP Download Script
  3. #By Juvenal
  4. #Uploads files matching the extension filter from the working directory(or specified)
  5. #to the ftp server specified with the same directory structure
  6. use File::Basename;
  7. use File::Find;
  8. use Net::FTP;
  9. use Switch;
  10. use Cwd;
  11. #------------- DO NOT EDIT ABOVE THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING ------------
  12.  
  13. #User Editable Vars
  14. $filter = '.*';
  15. $rootdir = "/home/slice9/Downloads";
  16. $sourcedir = cwd();
  17. $ftp_usr = "user";
  18. $ftp_pass = "pass";
  19. $ftp_port = "21";
  20. $ftp_host = "remote.host";
  21. $ftp_root = "";
  22.  
  23. #------------- DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING ------------
  24.  
  25. #Variables
  26. @filelist;
  27. @dirlist;
  28. $debug = 1;
  29. $testing = 0;
  30. $ftp;
  31.  
  32. #Get Switches
  33. if ($#ARGV != -1)
  34. {
  35. foreach $argnum (0 .. $#ARGV)
  36. {
  37. my $arg = $ARGV[$argnum];
  38. my $narg = $ARGV[$argnum + 1];
  39. switch (lc $arg)
  40. {
  41. case "-s" { $sourcedir = $narg }
  42. case "-h" { $ftp_host = $narg }
  43. case "-t" { $ftp_port = $narg }
  44. case "-u" { $ftp_usr = $narg }
  45. case "-r" { $ftp_root = $narg }
  46. case "-p" { $ftp_pass = $narg }
  47. case "-f" { $filter = $narg }
  48. case "-l" { $rootdir = $narg }
  49. case "-help" { Help() }
  50. }
  51. }
  52. }
  53.  
  54. #List Directory
  55. find({wanted => \&HandleFile, no_chdir => 1}, $sourcedir);
  56. RemoveEmptyDirs();
  57. if ($debug)
  58. {
  59. print "Out:\n";
  60. foreach $ent (@filelist)
  61. {
  62. print "$ent\n";
  63. }
  64. foreach $ent (@dirlist)
  65. {
  66. print "$ent\n";
  67. }
  68. }
  69.  
  70. #Open FTP
  71. $ftp = Net::FTP->new($ftp_host, Port => $ftp_port)
  72. or die "Cannot Connect to FTP Server: $@";
  73. $ftp->login($ftp_usr, $ftp_pass)
  74. or die "Cannot Login to FTP Server: ", $ftp->message;
  75. $ftp->cwd($ftp_root)
  76. or die "Cannot change to FTP Root: ", $ftp->message;
  77. $ftp->binary();
  78. foreach $ent (@dirlist)
  79. {
  80. print "Making Directory: $ent...\n";
  81. if ($testing eq 0)
  82. {
  83. $ftp->mkdir($ftp_root.$ent, 1)
  84. or die "Cannot Make Directory $ent: ", $ftp->message;
  85. }
  86. print "DONE\n"
  87. }
  88. foreach $ent (@filelist)
  89. {
  90. $fsize = -s $rootdir.$ent;
  91. $fsz = $fsize / 1024;
  92. $fsz = sprintf("%.2f", $fsz);
  93. print "Sending File(".$fsz."K): $ent...\n";
  94. SetHash($fsize);
  95. if ($testing eq 0)
  96. {
  97. $ftp->put($rootdir.$ent, $ftp_root.$ent)
  98. or die "Cannot Put File $ent: ", $ftp->message;
  99. }
  100. print "DONE\n";
  101. }
  102.  
  103. $ftp->quit();
  104. #Exit
  105. exit(0);
  106.  
  107. #Functions
  108. sub RemoveEmptyDirs()
  109. {
  110. my @tmpdlist = @dirlist;
  111. my @tmplist;
  112. foreach $ent (@filelist)
  113. {
  114. foreach $dir (@tmpdlist)
  115. {
  116. if ($ent =~ /^$dir.*/)
  117. {
  118. if ($dir ne "") {
  119. push(@tmplist, $dir);
  120. }
  121. my( $index )= grep { $tmpdlist[$_] eq $dir } 0..$#tmpdlist;
  122. delete $tmpdlist[$index];
  123. }
  124. }
  125. }
  126. @dirlist = @tmplist;
  127. }
  128. sub HandleFile()
  129. {
  130. my $filename = $_;
  131. if (-f $filename)
  132. {
  133. $filename =~ s/$rootdir//;
  134. if ($filename =~ /.*\.($filter)/)
  135. {
  136. push(@filelist, $filename);
  137. }
  138. }
  139. if (-d $filename)
  140. {
  141. $filename =~ s/$rootdir//;
  142. push(@dirlist, $filename);
  143. }
  144. }
  145. sub SetHash($)
  146. {
  147. my $fsize = $_[0];
  148. my $hashval = 1024000;
  149. $hashval = $fsize / 50;
  150. $ftp->hash(STDOUT,$hashval);
  151. }
  152. sub Help()
  153. {
  154. print "Usage: DownloadFiles \[Switches\]\n";
  155. print "EX: DownloadFiles -h ftp.my.com -u JoeSmith -p 12345 -r /Download/Seedbox/\n\n";
  156. print "Switches:\n";
  157. print "-s Source Directory, Where to read files from\n";
  158. print "-f Extension Regex Filter (Default .*) Separate with | Ex:\n";
  159. print " rar|bin|txt|d.*\n";
  160. print "-l Local Root Directory (Replaced by FTP Root in file paths)\n";
  161. print "-h FTP Host\n";
  162. print "-t FTP Port\n";
  163. print "-u FTP User\n";
  164. print "-p FTP Password\n";
  165. print "-r FTP Root Directory\n";
  166. print "\n\n";
  167. print "All Switches can have defaults set by editing the script\n";
  168. print "Other than Source Directory, which defaults to the Working Directory\n";
  169. exit(0);
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement