################################################################################################# ## Purpose: This script logs into the rbc ftp server and downloads data files, then moves them ## ## to a shared folder so that they can be synced with the quickbooks data. ## ## Author: Nick Peters ## ## Pre: Needs a couple of arguments at run time: ## ## 1) What batch we want to run: ns (netset) or ## ## inl (inetlink). ## ## 2) The Working Directory we want to save the ## ## downloaded content too. ## ## 3) The location of a config file containing ## ## hashed user name and passwords for the ## ## different batch runs. ## ################################################################################################# use warnings; use strict; use Net::FTPSSL; use Crypt::CBC; use Email::Send; use Email::Simple::Creator; my($day, $month, $year)=(localtime)[3,4,5]; $month = $month + 1; $year = $year + 1900; my $date = "$year-$month-$day"; my $log_name = "rbc_ftp_log_$date.log"; open (STDERR, ">>", $log_name); my $num_args = $#ARGV + 1; if ($num_args == 3) { my $batch = $ARGV[0]; my $work_dir = $ARGV[1]; my $config = $ARGV[2]; #time to read in the config file: open(CONFIG, "<$config") or die "Error Opening Config File!!!\n"; my @config_lines = ; close(CONFIG); my $cipher = Crypt::CBC->new( -key => 'xxxxxxxx', -cipher => 'Blowfish',); my $user; my $pass; if ($batch eq "ns") { #we are doing a netset batch. $user = $cipher->decrypt($config_lines[0]); $pass = $cipher->decrypt($config_lines[1]); } if ($batch eq "inl") { #we are doing a inetlink batch. $user = $cipher->decrypt($config_lines[2]); $pass = $cipher->decrypt($config_lines[3]); } my $host = "xxx.xxxx.xxx"; my $ftp = Net::FTPSSL->new($host, Port => 21, Encryption => 'E', Debug => 1) or die "Cannot connect to $host: $@"; #connect to the ftp server. #lets do the inl stuff first: $ftp->login($user, $pass) or die "Can't Login: ", $ftp->message; #change working directory on the remote host: $ftp->cwd("outbound/AAPA/") or die "Can't change working directory: ", $ftp->message; my @dir_listing = $ftp->nlst() or die "Can't List Files.... ", $ftp->message; $ftp->binary(); foreach (@dir_listing) { #loop through files in the directory and download the ones that do not have downloaded in the name. if ( m/RPT0900P\.\d\d\d.downloaded/ ) { #already seen this file, output to log print STDERR "$date : Found File (Seen It Already): $_ \n"; } else { #haven't seen this file before: print STDERR "Found File (New): $_ \n"; print STDERR "Changing Local Working Directory to: $work_dir\n"; chdir($work_dir) or die "Couldn't change local working directory!!!\n"; $ftp->get($_) or die "Couldn't Download New File: $_ Because: ", $ftp->message; } } $ftp->quit; } else { print "Error, i need some command line arguments!!\n"; print "Usage: rbc_ftp.pl \n"; print " is equal to what run we want to run, ie: 'ns' for NetSet or 'inl' for I-NetLink\n"; print " is equal to the directory we want to save the downloaded files in.\n"; print " is equal to the location of the config file.\n"; } my $mailer = Email::Send->new( { mailer => 'SMTP::TLS', mailer_args => [ Host => 'smtp.gmail.com', #Port => 587, User => 'xxx.xxxx@xxxx.xx', Password=> 'xxxxxxxx', Hello => 'localhost', ] } ); my $email = Email::Simple->create( header => [ From => 'xxxx.xxxx@xxxx.xx', To => 'xxxxxx@xxxx.xxx', Subject => 'RBC FTP Status:', ], body => 'This is a test.....', ); eval { $mailer->send($email) }; die "Error sending email: $@" if $@; close(STDERR);