Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- use Net::FTP;
- use strict;
- use Term::ANSIColor qw(:constants);
- $Term::ANSIColor::AUTORESET = 1;
- my @RAC = ("host","user","pass"); # RAC Login details (host, user, pass)
- my @elm = ("elm","",""); ###
- my @oak = ("oak","",""); # Prolog Login details (host, user, pass) if user is empty then prompt for password
- my @birch = ("birch","",""); ###
- my $fleetdir = "./fleet_cards"; ###
- my $mmdir = "./mm_cards"; #
- my $giftdir = "./incentives"; # Directories where the files reside.
- my $responddir = "./welcome_admin"; #
- my $cmmdir = "./price_review"; #
- my $prologdir = "/workfiles/input/RAC"; #
- #my $testdir = "/home/ops/test"; ###
- my $fleetre = "commercial.txt_[0-9]{8}_[0-9]{6}"; ###
- my $mmre = "motorman.txt_[0-9]{8}_[0-9]{6}"; #
- my $mmbre = "motorman-branded.txt_[0-9]{8}_[0-9]{6}"; # Regular expressions to recognise
- my $giftre = "PROL_INCENT.txt_[0-9]{8}_[0-9]{6}"; # each different file.
- my $respondre = "PROL_WELAD.txt_[0-9]{8}_[0-9]{6}"; # Matches the name and the trailing date and time
- my $cmmre = "PROL_CMMAPR_[0-9]{8}_[0-9]{6}"; #
- #my $testre = "roflrofl.txt_[0-9]{8}_[0-9]{6}"; #
- my @allre = ($fleetre,$mmre,$mmbre,$giftre,$respondre,$cmmre); ###
- my $fleetprefix = "FL"; ###
- my $mmprefix = "MM"; #
- my $mmbprefix = "MMB"; # Prefixes to save the file on oak, (prefix+date e.g RS20100501)
- my $giftprefix = "GT"; #
- my $respondprefix = "RS"; #
- my $cmmprefix = "CMM"; #
- #my $testprefix = "RR"; ###
- my @allprefix = ($fleetprefix,$mmprefix,$mmbprefix,$giftprefix, $cmmprefix, $respondprefix);
- my @downloadedfiles;
- download(@RAC, $fleetdir, $fleetre, $fleetprefix);
- download(@RAC, $mmdir, $mmre, $mmprefix);
- download(@RAC, $mmdir, $mmbre, $mmbprefix);
- download(@RAC, $giftdir, $giftre, $giftprefix);
- download(@RAC, $responddir, $respondre, $respondprefix);
- download(@RAC, $cmmdir, $cmmre, $cmmprefix);
- if(filestoupload(@allprefix))
- {
- upload(@oak, $prologdir, @allprefix);
- }
- else
- {
- print BOLD RED "[-] No files waiting to be uploaded, quitting...\n\n";
- }
- sub download
- {
- my @filelist;
- my @files;
- my $ftp;
- my($lhost, $luser, $lpass, $ldir, $lre, $lprefix) = @_;
- if($luser eq "")
- {
- print "Enter username for ", MAGENTA "$lhost", RESET ": ";
- chomp($luser = <STDIN>);
- unless($luser)
- {
- $luser = "anonymous";
- }
- print "Enter password for ", MAGENTA "$luser", RESET ": ";
- system('stty','-echo');
- chomp($lpass=<STDIN>);
- system('stty','echo');
- print "\n\n";
- }
- $ftp = Net::FTP->new($lhost, Debug => 0)
- or die BOLD RED "[-] Failed to connect to $lhost: $@";
- print "[+] Connected to $lhost...\n";
- $ftp->login($luser,$lpass)
- or die BOLD RED "[-] Login failed", RESET, "(has password changed?): ", $ftp->message;
- print "[+] Logged in successfully\n";
- $ftp->cwd("$ldir")
- or die BOLD RED "[-] Cannot change to $ldir", $ftp->message;
- print "[+] Changed directory to $ldir\n";
- $ftp->ascii();
- @filelist = $ftp->ls();
- my $bool;
- $bool = 1;
- foreach(@filelist)
- {
- if(m/$lre/i)
- {
- if($bool == 1)
- {
- print "\n[+] Matched file(s): \n";
- $bool = 0;
- }
- print BOLD GREEN "$_\n";
- push (@files,$_);
- }
- }
- print "\n";
- if(@files)
- {
- foreach(@files)
- {
- my $filedate;
- my $tempfile;
- my $temp = $_;
- $temp =~ m/([0-9]{8})/ or die BOLD RED "[-] Filename not in correct format";
- $filedate = $1;
- my $isDupe = 0;
- my $dupfile = "$lprefix$filedate";
- my @duplicatefiles = `ls -1 | grep -e $dupfile`;
- print "[+] Testing whether $temp is fully uploaded\n";
- print "[+] Getting size...\n";
- my $filesize = $ftp->size($temp);
- print "[+] $temp is $filesize bytes\n";
- print "[+] Sleeping for 1 second...\n";
- sleep(1);
- print "[+] Getting filesize again\n";
- if($filesize < $ftp->size($temp))
- {
- print BOLD YELLOW "[!] Filesize has increased, waiting for it to finish\n";
- while ($filesize < $ftp->size($temp))
- {
- print BOLD YELLOW "[!] File still hasn't finished being uploaded\n";
- print "[+] Sleeping for 1 second...\n";
- $filesize = $ftp->size($temp);
- sleep(1);
- }
- }
- print "[+] File has finished being uploaded, ok to download!\n";
- my $postfix = 0;
- if(-e $dupfile)
- {
- $isDupe = 1;
- while ($isDupe == 1)
- {
- if(-e "$dupfile-$postfix")
- {
- $postfix++;
- }
- else
- {
- $ftp->get($temp,"$dupfile-$postfix.TXT") or die BOLD RED "Could not download $temp: ", $ftp->message;
- print BOLD GREEN "[+] Saved $temp as $dupfile-$postfix.TXT in " . `pwd`;
- $ftp->rename($temp, "Processed/$temp")
- or print BOLD RED "[-] Failed to put $temp in processed\n";
- $isDupe = 0;
- }
- }
- }
- else
- {
- $ftp->get($temp,"$dupfile.TXT") or die BOLD RED "Could not download $temp: ", $ftp->message;
- print BOLD GREEN "[+] Saved $temp as $dupfile.TXT in " . `pwd`;
- if($ftp->rename($temp, "Processed/$temp"))
- {
- print "[+] $temp has been moved to processed\n\n";
- }
- else
- {
- print BOLD RED "[-] Failed to put $temp in processed\n\n";
- }
- }
- }
- }
- else
- {
- print BOLD RED "[-] No files matched in $ldir\n\n";
- }
- $ftp->quit();
- return @files;
- }
- sub upload
- {
- my $ftp;
- my($lhost, $luser, $lpass, $ldir, @lre) = @_;
- if($luser eq "")
- {
- print "Enter username for ", MAGENTA "$lhost", RESET ": ";
- chomp($luser = <STDIN>);
- unless($luser)
- {
- $luser = "anonymous";
- }
- print "Enter password for ", MAGENTA "$luser", RESET ": ";
- system('stty','-echo');
- chomp($lpass=<STDIN>);
- system('stty','echo');
- print "\n\n";
- }
- $ftp = Net::FTP->new($lhost, Debug => 0)
- or die BOLD RED "[-] Cannot connect to $lhost: $@";
- print "[+] Connected to $lhost...\n";
- $ftp->login($luser,$lpass)
- or die BOLD RED "[-] Login failed", RESET, "(has password changed?): ", $ftp->message;
- print "[+] Logged in successfully\n";
- $ftp->cwd("$ldir")
- or die BOLD RED "[-] Cannot change to $ldir", $ftp->message;
- print "[+] Changed directory to $ldir\n";
- #my @dirlist = `ls -l`;
- my $file;
- #my $tempre;
- #foreach $file (@dirlist)
- #{
- # chomp $file;
- # #print "\n$file\n";
- # foreach $tempre (@lre)
- # {
- # #print "\n$tempre\n";
- # if($file =~ m/.*($tempre.*)/)
- # {
- # my $filetoupload = $1;
- # if($ftp->put($filetoupload))
- # {
- # print BOLD GREEN "[+] $filetoupload uploaded to $ldir on $lhost successfully\n";
- # `rm $filetoupload`;
- # }
- # else
- # {
- # print BOLD RED "[-] Failed to upload $filetoupload: " . $ftp->message;
- # $ftp->delete($filetoupload);
- # }
- # }
- # }
- #}
- #print @lre;
- my @filestoupload = filestoupload(@lre);
- #print @filestoupload;
- #if (@filestoupload)
- #{
- foreach $file (@filestoupload)
- {
- `cp "$file" "./backup"`;
- if($ftp->put($file))
- {
- print BOLD GREEN "\n[+] $file uploaded to $ldir on $lhost successfully\n";
- `rm $file`;
- }
- else
- {
- print BOLD RED "\n[-] Failed to upload $file: " . $ftp->message;
- #$ftp->delete($file);
- }
- }
- #}
- $ftp->quit();
- }
- sub filestoupload
- {
- my @lre = @_;
- my @dirlist = `ls -l`;
- my $file;
- #my $re;
- my $tempre;
- my @files;
- foreach $file (@dirlist)
- {
- chomp $file;
- #print "\n$file\n";
- foreach $tempre (@lre)
- {
- #print "\n$tempre\n";
- #$re = "\$\($tempre[0-9]{8}.TXT\)";
- if($file =~ m/\s($tempre[0-9]{8}.TXT)/)
- {
- push @files, $1;
- }
- }
- }
- return @files;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement