Advertisement
Guest User

Untitled

a guest
Jun 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.46 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use Net::FTP;
  4. use strict;
  5. use Term::ANSIColor qw(:constants);
  6.  
  7. $Term::ANSIColor::AUTORESET = 1;
  8.  
  9. my @RAC = ("host","user","pass"); #  RAC Login details (host, user, pass)
  10.  
  11.  
  12. my @elm = ("elm","","");        ###
  13. my @oak = ("oak","","");        #  Prolog Login details (host, user, pass) if user is empty then prompt for password
  14. my @birch = ("birch","","");        ###
  15.  
  16. my $fleetdir = "./fleet_cards";         ###
  17. my $mmdir = "./mm_cards";           #
  18. my $giftdir = "./incentives";           #  Directories where the files reside.
  19. my $responddir = "./welcome_admin";     #
  20. my $cmmdir = "./price_review";          #
  21. my $prologdir = "/workfiles/input/RAC";     #
  22. #my $testdir = "/home/ops/test";        ###
  23.  
  24. my $fleetre =   "commercial.txt_[0-9]{8}_[0-9]{6}";     ###
  25. my $mmre    =   "motorman.txt_[0-9]{8}_[0-9]{6}";       #
  26. my $mmbre   =   "motorman-branded.txt_[0-9]{8}_[0-9]{6}";   #  Regular expressions to recognise
  27. my $giftre  =   "PROL_INCENT.txt_[0-9]{8}_[0-9]{6}";        #  each different file.
  28. my $respondre   =   "PROL_WELAD.txt_[0-9]{8}_[0-9]{6}";     #  Matches the name and the trailing date and time
  29. my $cmmre   =   "PROL_CMMAPR_[0-9]{8}_[0-9]{6}";        #
  30. #my $testre =   "roflrofl.txt_[0-9]{8}_[0-9]{6}";       #
  31. my @allre   = ($fleetre,$mmre,$mmbre,$giftre,$respondre,$cmmre);    ###
  32.  
  33. my $fleetprefix =   "FL";           ###
  34. my $mmprefix    =   "MM";           #
  35. my $mmbprefix   =   "MMB";          #  Prefixes to save the file on oak, (prefix+date e.g RS20100501)
  36. my $giftprefix  =   "GT";           #
  37. my $respondprefix = "RS";           #
  38. my $cmmprefix   =   "CMM";          #
  39. #my $testprefix =   "RR";           ###
  40. my @allprefix   =   ($fleetprefix,$mmprefix,$mmbprefix,$giftprefix, $cmmprefix, $respondprefix);
  41.  
  42. my @downloadedfiles;
  43.  
  44. download(@RAC, $fleetdir, $fleetre, $fleetprefix);
  45. download(@RAC, $mmdir, $mmre, $mmprefix);
  46. download(@RAC, $mmdir, $mmbre, $mmbprefix);
  47. download(@RAC, $giftdir, $giftre, $giftprefix);
  48. download(@RAC, $responddir, $respondre, $respondprefix);
  49. download(@RAC, $cmmdir, $cmmre, $cmmprefix);
  50.  
  51.  
  52.  
  53. if(filestoupload(@allprefix))
  54. {
  55.     upload(@oak, $prologdir, @allprefix);
  56. }
  57. else
  58. {
  59.     print BOLD RED "[-] No files waiting to be uploaded, quitting...\n\n";
  60. }
  61.  
  62. sub download
  63. {
  64.     my @filelist;
  65.     my @files;
  66.     my $ftp;
  67.    
  68.     my($lhost, $luser, $lpass, $ldir, $lre, $lprefix) = @_;
  69.     if($luser eq "")
  70.     {
  71.         print "Enter username for ", MAGENTA "$lhost", RESET ": ";
  72.         chomp($luser = <STDIN>);
  73.         unless($luser)
  74.         {
  75.             $luser = "anonymous";
  76.         }
  77.         print "Enter password for ", MAGENTA "$luser", RESET ": ";
  78.         system('stty','-echo');
  79.         chomp($lpass=<STDIN>);
  80.         system('stty','echo');
  81.         print "\n\n";
  82.     }
  83.     $ftp = Net::FTP->new($lhost, Debug => 0)
  84.         or die BOLD RED "[-] Failed to connect to $lhost: $@";
  85.    
  86.     print "[+] Connected to $lhost...\n";
  87.  
  88.     $ftp->login($luser,$lpass)
  89.         or die BOLD RED "[-] Login failed", RESET, "(has password changed?): ", $ftp->message;
  90.     print "[+] Logged in successfully\n";
  91.     $ftp->cwd("$ldir")
  92.         or die BOLD RED "[-] Cannot change to $ldir", $ftp->message;
  93.     print "[+] Changed directory to $ldir\n";
  94.     $ftp->ascii();
  95.     @filelist = $ftp->ls();
  96.    
  97.     my $bool;
  98.     $bool = 1;
  99.    
  100.     foreach(@filelist)
  101.     {
  102.        
  103.         if(m/$lre/i)
  104.         {
  105.             if($bool == 1)
  106.             {
  107.                 print "\n[+] Matched file(s): \n";
  108.                 $bool = 0;
  109.             }
  110.             print BOLD GREEN "$_\n";
  111.             push (@files,$_);
  112.         }
  113.  
  114.     }
  115.             print "\n";
  116.     if(@files)
  117.     {
  118.         foreach(@files)
  119.         {
  120.             my $filedate;
  121.             my $tempfile;
  122.             my $temp = $_;
  123.             $temp =~ m/([0-9]{8})/ or die BOLD RED "[-] Filename not in correct format";
  124.             $filedate = $1;
  125.             my $isDupe = 0;
  126.             my $dupfile = "$lprefix$filedate";
  127.             my @duplicatefiles = `ls -1 | grep -e $dupfile`;
  128.             print "[+] Testing whether $temp is fully uploaded\n";
  129.             print "[+] Getting size...\n";
  130.             my $filesize = $ftp->size($temp);
  131.             print "[+] $temp is $filesize bytes\n";
  132.             print "[+] Sleeping for 1 second...\n";
  133.             sleep(1);
  134.             print "[+] Getting filesize again\n";
  135.             if($filesize < $ftp->size($temp))
  136.             {
  137.                 print BOLD YELLOW "[!] Filesize has increased, waiting for it to finish\n";
  138.                 while ($filesize < $ftp->size($temp))
  139.                 {
  140.                     print BOLD YELLOW "[!] File still hasn't finished being uploaded\n";
  141.                     print "[+] Sleeping for 1 second...\n";
  142.                     $filesize = $ftp->size($temp);
  143.                     sleep(1);
  144.                 }
  145.             }
  146.             print "[+] File has finished being uploaded, ok to download!\n";
  147.             my $postfix = 0;
  148.             if(-e $dupfile)
  149.             {
  150.                 $isDupe = 1;
  151.                 while ($isDupe == 1)
  152.                 {
  153.                     if(-e "$dupfile-$postfix")
  154.                     {
  155.                         $postfix++;
  156.                     }
  157.                     else
  158.                     {
  159.                         $ftp->get($temp,"$dupfile-$postfix.TXT") or die BOLD RED "Could not download $temp: ", $ftp->message;
  160.                         print BOLD GREEN "[+] Saved $temp as $dupfile-$postfix.TXT in " . `pwd`;
  161.                         $ftp->rename($temp, "Processed/$temp")
  162.                             or print BOLD RED "[-] Failed to put $temp in processed\n";
  163.                         $isDupe = 0;
  164.                     }
  165.                    
  166.                 }
  167.             }
  168.             else
  169.             {
  170.                 $ftp->get($temp,"$dupfile.TXT") or die BOLD RED "Could not download $temp: ", $ftp->message;
  171.                 print BOLD GREEN "[+] Saved $temp as $dupfile.TXT in " . `pwd`;
  172.                 if($ftp->rename($temp, "Processed/$temp"))
  173.                 {
  174.                     print "[+] $temp has been moved to processed\n\n";
  175.                 }
  176.                 else
  177.                 {
  178.                     print BOLD RED "[-] Failed to put $temp in processed\n\n";
  179.                 }
  180.             }
  181.            
  182.         }
  183.     }
  184.     else
  185.     {
  186.         print BOLD RED "[-] No files matched in $ldir\n\n";
  187.     }
  188.     $ftp->quit();
  189.     return @files;
  190.    
  191. }
  192.  
  193. sub upload
  194. {
  195.     my $ftp;
  196.     my($lhost, $luser, $lpass, $ldir, @lre) = @_;
  197.    
  198.     if($luser eq "")
  199.         {
  200.             print "Enter username for ", MAGENTA "$lhost", RESET ": ";
  201.             chomp($luser = <STDIN>);
  202.             unless($luser)
  203.             {
  204.                 $luser = "anonymous";
  205.             }
  206.             print "Enter password for ", MAGENTA "$luser", RESET ": ";
  207.             system('stty','-echo');
  208.             chomp($lpass=<STDIN>);
  209.             system('stty','echo');
  210.             print "\n\n";
  211.         }
  212.         $ftp = Net::FTP->new($lhost, Debug => 0)
  213.             or die BOLD RED "[-] Cannot connect to $lhost: $@";
  214.        
  215.         print "[+] Connected to $lhost...\n";
  216.    
  217.         $ftp->login($luser,$lpass)
  218.             or die BOLD RED "[-] Login failed", RESET, "(has password changed?): ", $ftp->message;
  219.         print "[+] Logged in successfully\n";
  220.         $ftp->cwd("$ldir")
  221.             or die BOLD RED "[-] Cannot change to $ldir", $ftp->message;
  222.     print "[+] Changed directory to $ldir\n";
  223.    
  224.     #my @dirlist = `ls -l`;
  225.     my $file;
  226.     #my $tempre;
  227.     #foreach $file (@dirlist)
  228.     #{ 
  229.     #   chomp $file;
  230.     #   #print "\n$file\n";
  231.     #   foreach $tempre (@lre)
  232.     #   {  
  233.     #       #print "\n$tempre\n";
  234.     #       if($file =~ m/.*($tempre.*)/)
  235.     #       {
  236.     #           my $filetoupload = $1;
  237.     #           if($ftp->put($filetoupload))
  238.     #           {
  239.     #               print BOLD GREEN "[+] $filetoupload uploaded to $ldir on $lhost successfully\n";
  240.     #               `rm $filetoupload`;
  241.     #           }
  242.     #           else
  243.     #           {
  244.     #               print BOLD RED "[-] Failed to upload $filetoupload: " . $ftp->message;
  245.     #               $ftp->delete($filetoupload);
  246.     #           }
  247.     #       }
  248.     #   }
  249.     #}
  250.     #print @lre;
  251.     my @filestoupload = filestoupload(@lre);
  252.     #print @filestoupload;
  253.     #if (@filestoupload)
  254.     #{
  255.         foreach $file (@filestoupload)
  256.         {
  257.             `cp "$file" "./backup"`;
  258.             if($ftp->put($file))
  259.             {
  260.                 print BOLD GREEN "\n[+] $file uploaded to $ldir on $lhost successfully\n";
  261.                 `rm $file`;
  262.             }
  263.             else
  264.             {
  265.                 print BOLD RED "\n[-] Failed to upload $file: " . $ftp->message;
  266.                 #$ftp->delete($file);
  267.             }
  268.         }
  269.     #}
  270.     $ftp->quit();
  271. }
  272.  
  273. sub filestoupload
  274. {
  275.     my @lre = @_;
  276.     my @dirlist = `ls -l`;
  277.     my $file;
  278.     #my $re;
  279.     my $tempre;
  280.     my @files;
  281.     foreach $file (@dirlist)
  282.     {  
  283.         chomp $file;
  284.         #print "\n$file\n";
  285.         foreach $tempre (@lre)
  286.         {  
  287.             #print "\n$tempre\n";
  288.             #$re = "\$\($tempre[0-9]{8}.TXT\)";
  289.             if($file =~ m/\s($tempre[0-9]{8}.TXT)/)
  290.             {
  291.                 push @files, $1;
  292.             }
  293.         }
  294.     }
  295.     return @files;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement