salatconed has asked for the wisdom of the Perl Monks concerning the following question:

I am having an issue with running a system call after I finish a FTP file transfer. The FTP call transfers the file correctly, but the decryption system call fails. The app logis intoa ftp server and download all the files to a temp directory, then decrypts each one and creates a single plain test file.code lines 135 to 209

I have tested the code by placing the encrypted file in the temp directory and eliminating the FTP transfer, the system call works properly.

I also tested hardcoding the system call command into a batch file and simple call the batch file, and I get the same results. The call fails if its called after the FTP tranfer.

Does anyone see any errors with how I'm terminating the FTP tranfer ?

1 use Mail::Sender; 2 use File::Copy; 3 use Net::FTP; 4 use Getopt::Long; 5 use Cwd; 6 use strict; 7 8 # set global variables 9 my $logfile = "c:\\PGP_Scripts\\logfile.txt"; 10 my $_debug = 0; # 0 - no logging, 1 - log to file, 2 - +log to file and console 11 my $_help = ''; 12 my $dh; # Handle to working directory 13 my $local_file = ''; 14 my $local_dir = "c:/PGP_temp/"; 15 my $current_file = ''; #file name being worked on 16 my $archive_file = ''; 17 my $ftp_file = ''; 18 my $ftp_session = ''; 19 my $pgp_call = ''; # Variable holds command line string to + run PGP application 20 my $pgp_file = ''; # File name and full path of encrypted +file 21 my $u_pgp_file = ''; # Name of unprotected file from un-encr +ypt operation 22 my $file_time = ''; # Holds current time string 23 my $pgp_file_tmp = ''; 24 25 # PGP encryption encryption variables 26 my $pgp_app = '"C:\\Program Files\\ArticSoft\\FileAssu +rity OpenPGP CLS\\bin\\scriptor.exe"'; # Full path and name of the ec +ryptioin application 27 my $pgp_arg_encrypt = '"PGP_Account <PGP_Account@company.com>" +'; # Define the encrypted file recipient 28 my $pgp_arg_sign = '"My Company <me@mycompany.com>"'; + # Define the encrypted file sender 29 my $pgp_arg_autologin = 'passsword'; + # Login credentials to run the encryption application 30 31 # Command line argument variables 32 my $action = ''; # send or get files fom ftp server 33 my $ftp_server = ''; 34 my $work_dir = getcwd; 35 my $site_id = ''; 36 my $site_pwd = ''; 37 38 # -------------------------------------------------------------- +--------------------------- 39 40 # Test command line arguments 41 GetOptions( 42 'action=s' => \$action, 43 'server=s' => \$ftp_server, 44 'siteid=s' => \$site_id, 45 'sitepwd=s' => \$site_pwd, 46 'workdir=s' => \$work_dir, 47 'debug=i' => \$_debug, 48 "help|?" => \$_help, 49 ); 50 51 if($_help) { HelpMessage( ); } 52 53 if(!$action) { 54 print "--action is a required field!!!!\n\n"; 55 HelpMessage( ); 56 } 57 58 if(!$ftp_server) { 59 print "--server is a required field!!!!\n\n"; 60 HelpMessage( ); 61 } 62 63 if(!$site_id) { 64 print "--siteid is a required field!!!!\n\n"; 65 HelpMessage( ); 66 } 67 68 if(!$site_pwd) { 69 print "--sitepwd is a required field!!!!\n\n"; 70 HelpMessage( ); 71 } 72 73 if(!$work_dir) { 74 print "--workdir is a required field!!!!\n\n"; 75 HelpMessage( ); 76 } 77 78 logit("\n\n Start file transfer process"); 79 80 if($_debug > 1){ 81 logit("Working directory : $work_dir"); 82 logit("FTP server : $ftp_server"); 83 logit("FTP logon ID : $site_id"); 84 logit("FTP logon PWD : $site_pwd"); 85 } 86 87 # --------------------------------------------- 88 # --------- Main Logic ------------------ 89 # --------------------------------------------- 90 91 # check if trailing slash exists otherwise add it. 92 if(substr($work_dir,-1,1) ne '/'){ 93 $work_dir = $work_dir . '/'; 94 } 95 96 # Substitute back-slahes with forward-slashes, to make it protab +le between *nix and Windows 97 $work_dir =~ s/\\/\//g; 98 if($_debug > 1){ 99 logit("Normalized working directory: $work_dir"); 100 } 101 102 #Connect to FTP site 103 $ftp_session = Net::FTP->new($ftp_server, Debug => 0, Passive = +> 1) 104 105 106 #Login to FTP server 107 $ftp_session->login($site_id, $site_pwd) 108 109 110 #------------------------------------------------ 111 # Get all file's on FTP server, put in work_dir 112 #------------------------------------------------ 113 if($action eq "get"){ 114 115 if($_debug){ 116 logit("-----------------------------------"); 117 logit(" Get files "); 118 logit("-----------------------------------"); 119 } 120 121 # Delete any existing files in the temp directory 122 opendir( $dh, $local_dir) or die "Cant create handle: $!"; 123 while( readdir $dh){ 124 if(-f $local_dir.$_){ 125 logit("Deleting file: $local_dir$_"); 126 unlink ($local_dir.$_); 127 } 128 } 129 close($dh); 130 131 my $file_quty = 0; 132 #Download files to local temp directory 133 134 foreach $ftp_file ($ftp_session->ls("*")) { 135 if($_debug){ 136 logit("File found on ftp server: $ftp_file"); 137 } 138 $pgp_file_tmp = $local_dir . $ftp_file; 139 140 141 142 $file_quty = $file_quty + 1; 143 } 144 145 $ftp_session->quit; 146 147 #If no files were downloaded exit. 148 if($file_quty == 0) { 149 logit("No files to process"); 150 exit; 151 } 152 else{ 153 logit("Number of files downloaded: $file_quty"); 154 } 155 156 #See log 6/29/2012 157 sleep(5); 158 159 # Create PGP unprotect system call 160 161 162 163 $file_quty = 0; 164 # Process each file in the local temp directory 165 opendir( $dh, $local_dir) or die "Cant create handle: $!"; 166 while( readdir $dh){ 167 $pgp_file = $local_dir . $_; # Encrypted + file name with full path 168 169 # Create file name for incoming data 170 my $file_time = gettimestamp(); 171 $file_time =~ s/\:/-/g; # Substitute colon's with d +ashes 172 $file_time =~ s/\[/_/g; # Substitute brace with und +erscore 173 $file_time =~ s/\]//g; # Substitute brace with und +erscore 174 my $backup_file = $local_dir . "old\\" . $file_time.$_; 175 176 # Process if object is a file 177 if( -f $pgp_file){ 178 179 unless ( $pgp_file =~ m/\.PGP/){ 180 next; # Not a PGP fil +e, read the next file 181 } 182 183 if($_debug){ 184 logit("---------- Processing file $pgp_file + -------------"); 185 } 186 187 188 # Edit file name to remove cahracters after the PGP + extension. 189 # Test if file name ends has numbers after PGP exte +nsion. 190 if($pgp_file =~ m/[0-9]$/){ 191 $pgp_file_tmp = $pgp_file; 192 $pgp_file =~ s/\.PGP.*$/\.PGP/g; # Delete any +characters after the PGP extension 193 logit("File name changed from <$pgp_file_tmp> t +o <$pgp_file>"); 194 if(copy($pgp_file_tmp, $pgp_file)){ 195 unlink ($pgp_file_tmp); 196 } else { 197 logit("Unable to rename file $pgp_file_tmp +to $pgp_file"); 198 } 199 } 200 201 # Un-encrypt file 202 my $unprotect_cmd = $pgp_app . " -unprotect -autolo +gin " . $pgp_arg_autologin . " -log append C:/PGP_temp/log/PGP_Job.ht +ml " . "\"" . $pgp_file . "\""; 203 if($_debug){ 204 logit("command: $unprotect_cmd"); 205 } 206 system `$unprotect_cmd`; 207 # system `c:\pgp_scripts\decrypt.bat`; #Edit +> July 3, 2012 208 209 210 sleep(5); 211 212 # Move current PGP file to "old" directory 213 if(copy($pgp_file, $backup_file)){ 214 unlink ($pgp_file); 215 } else { 216 logit("Unable to rename file $pgp_file to proce +ssed_.$file_quty"); 217 } 218 219 $file_quty = $file_quty + 1; 220 } # End of file loop 221 222 } # End unprotect files inside directory loop 223 close($dh); 224 225 # Create file name for incoming data 226 my $file_time = gettimestamp(); 227 $file_time =~ s/\:/-/g; # Substitute colon's with dashe +s 228 $file_time =~ s/\[/_/g; # Substitute brace with undersc +ore 229 $file_time =~ s/\]//g; # Substitute brace with undersc +ore 230 231 $local_file = $local_dir . "FromMellonBank" . "\.TXT"; + # The name is static 232 my $work_file = $work_dir . "MELLONACH\.TXT"; + # Name of production file with full path 233 my $arch_file = $work_dir . "archive\\MELLONACH_" . $file_ +time . "\.TXT"; # Nmae of archive file with full path. 234 if($_debug) { 235 logit("Generated local file: $local_file"); 236 logit("Generated work file: $work_file"); 237 } 238 # Create new file to hold the un-encrypted data from all th +e incoming files 239 open( my $LFh, ">>$local_file") or die("$! Can't open $loca +l_file"); 240 241 logit("\n\n >>> Combine text files into one "); 242 #Loop through local directory and append all the unprotecte +d files to a single file 243 opendir( $dh, $local_dir) or die "Cant create handle: $!"; 244 while( readdir $dh){ 245 $pgp_file_tmp = $local_dir . $_; 246 logit(" reading file: $pgp_file_tmp"); 247 if($_ =~ m/^AH.*$/){ 248 logit("Appending $pgp_file_tmp to $local_file"); 249 copy($pgp_file_tmp, $LFh); 250 } 251 } 252 #close (LFh); # Close working unprotected file 253 close ($dh); 254 255 # Delete old file if it exists 256 if(-f $work_file){ 257 unlink($work_file); 258 } 259 260 #Test if file is empty 261 my $filesize = -s $local_file; 262 logit("File size: $filesize"); 263 if(! $filesize){ 264 logit("JOB FAILED, created an empty file"); 265 email("PGP file unencryption failed"); 266 exit; 267 } 268 269 # Copy file to working directory 270 copy( $local_file, $work_file) or logit("Unable to copy fil +e: $!\nlocal file: $local_file\nwork file: $work_file"); 271 272 #Copy file to archive with timestamp 273 copy( $local_file, $arch_file) or logit("Unable to copy fil +e: $!\nlocal file: $local_file\narchive file: $arch_file"); 274 275 email("Received and un-encrypted $file_quty files."); 276 logit("Received and un-encrypted $file_quty files."); 277 } 278 279 #------------------------------------------ 280 # Put all files from work_dir to ftp server 281 #------------------------------------------ 282 if($action eq "put"){ 283 284 if($_debug){ 285 logit("-----------------------------------"); 286 logit(" Send files "); 287 logit("-----------------------------------"); 288 } 289 290 # Create directory handle 291 opendir( $dh, $work_dir) or die "Cant create handle: $!"; 292 293 # Create PGP encryption system call 294 295 if($_debug > 1){ 296 logit("pgp encrypt arg: $pgp_arg_encrypt"); 297 logit("pgp sign arg : $pgp_arg_sign"); 298 logit("pgp login arg : $pgp_arg_autologin"); 299 } 300 301 my $file_quty = 0; 302 # Process each file in the working directory 303 while( readdir $dh){ 304 $current_file = $work_dir . $_; 305 $local_file = $local_dir.$_; 306 $archive_file = $work_dir."archive/".$_; 307 308 # Process if object is a file 309 if( -f $current_file){ 310 #copy file to local directory 311 copy($current_file, $local_file) or die "Unable to +copy file: $!"; 312 313 # Call PGP application 314 if($_debug){ 315 316 317 logit("File with path: $local_file"); 318 logit("command line:\n$pgp_app $pgp_call $local +_file"); 319 } 320 321 # Encrypt file 322 system( "$pgp_app", "$pgp_call", "$local_file"); 323 324 $pgp_file = $local_file.".pgp"; 325 326 #Test if file exists 327 if( -f $pgp_file ){ 328 #Send file 329 $ftp_session->put($pgp_file) or die "Unable to +FTP PUT: $!" ; 330 logit("$local_file: \n$ftp_session->message"); 331 332 # Delete temp files 333 unlink $local_file or warn "Couldnt remove $loc +al_file: $!"; 334 unlink $pgp_file or warn "Couldnt remove $pgp_f +ile: $!"; 335 336 # Move original files to archive directory 337 copy($current_file, $archive_file) or warn "Una +ble to copy file: $!"; 338 unlink $current_file or warn "Couldnt remove $c +urrent_file: $!"; 339 340 $file_quty++; 341 } 342 else{ 343 logit("!!!!! ERROR unable to get $pgp_file f +ile !!!!!"); 344 email("!!!!! ERROR unable to get $pgp_file f +ile !!!!!"); 345 } 346 347 } 348 } 349 350 if( $file_quty > 0 ){ 351 logit ("Encrypted and sent $file_quty files."); 352 email ("Encrypted and sent $file_quty files."); 353 } else { 354 logit("No files to process"); 355 } 356 357 } 358 359 $ftp_session->quit; 360 361 exit; 362 363 # --------------------------------------------- 364 # --------- SubRoutines ------------------ 365 # --------------------------------------------- 366 367 sub logit { 368 my $line = shift; 369 open(LOG, ">>$logfile") or die("$! Can't open $logfile"); 370 my $timestamp = gettimestamp(); 371 $line = $timestamp."> ".$line; 372 print LOG "$line\n"; 373 close LOG; 374 if($_debug > 1 ){ 375 376 } 377 } 378 379 sub gettimestamp{ 380 my ($sec, $min, $hour, $day, $month, $yr19, @rest) = localt +ime(time); 381 my $tstamp = $month+1 . "-" . $day . "-" . ($yr19 + 1900) . + "[" . $hour . ":" . $min . ":" . $sec. "]"; 382 return $tstamp; 383 } 384 385 sub HelpMessage{ 386 print " 387 perl ftp_file.pl <--action ?> <--server ?> <--sitei +d ?> <--sitepwd ?> [--workdir ?] [--debug ?] [--help]\n\n 388 Command line arguments:\n 389 -----------------------\n 390 --action <get or put> direction in which the fi +les are going\n 391 --server Host name or IP address of FTP server\n 392 --siteid Logon ID\n 393 --sitepwd Pssword\n 394 --workdir Default is the current directory 395 for remote server use <\\\\server name\ +\share name> format\n 396 --debug 0 = default, 1 = log to file, 2 = log t +o file and screen\n 397 "; 398 exit; 399 } 400 401 sub email{ 402 my $sender = new Mail::Sender { 403 smtp => 'mailserver', 404 from => 'pgp@int.cecdes.com', 405 on_errors => undef, 406 } 407 or die "Can't create the Mail::Sender object: $Mail: +:Sender::Error\n"; 408 409 $sender->Open({ 410 to => 'me@mycompany.com', 411 subject => 'PGP encryption process', 412 }) 413 or die "Can't open the message: $sender->{'error_msg'}\ +n"; 414 415 # Put the text submitted to the subroutine in the body 416 $sender->SendLineEnc("@_"); 417 418 $sender->Close() or die "Failed to send the message: $sende +r->{'error_msg'}\n"; 419 420 }

Replies are listed 'Best First'.
Re: system command not working after ftp
by choroba (Cardinal) on Jul 06, 2012 at 14:48 UTC
    Are you sure you want to call system on the output of the command? That's what
    system `$command`;
    does. If not, remove the backquotes.
      I will remove the backticks and see how it goes. I have to wait until the next file comes in to see if it worked.
Re: system command not working after ftp
by Ransom (Beadle) on Jul 09, 2012 at 12:46 UTC

    Not an answer, but please wrap your large listings in "readmore" tags. It makes browsing this site much faster and easier.