CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
FTP Files from Linux to DB2

 



karthi_perl
Novice

Jan 1, 2012, 10:44 PM

Post #1 of 19 (2383 views)
FTP Files from Linux to DB2 Can't Post

Hi All,

I am newbie to this forum. I am also new to PERL script world. I have written a perl script to transfer three files from linux to db2. Here is the script.



#!/usr/bin/perl
#use strict;
use warnings;
use Net::FTP;
use Cwd;
use File::Basename;

################################
#Main Execution begins here
################################

$ftp = Net::FTP->new('ftp-utility.xx.xx.com', Debug => 0) or die "Couldn't connect to: $@\n";
$ftp->login('west.xxx.com','xxx') or die "Couldn't login to PSS server\n", $ftp->message;
$ftp->pwd();

print "change directory to /send\n";
$ftp->cwd(' send' ) or die "Couldn't change directory\n", $ftp->message;

#change directory to location of this perl file
chdir("/bis_data/msg/IProp_Data_Automation/Output/PSS");

#Get PSS Files
$ifile = 0;
@files = ('PLP301C1.TXT', 'PLP301C2.TXT', 'PLP302C1.TXT');
foreach(@files)
{
print "Attempting get $files[$ifile]\n";
#$ftp->put($files[$ifile]) or die "FTP get failed ", $ftp->message;
print "$files[$ifile] is copied to: ", Cwd::abs_path($files[$ifile]), "\n";
$ifile++;
}


$ftp->quit;

################



When i run the script, i don't get any error message. But i don't see those 3 files in the destination server.

is there any change needs to be done in the script? Inputs are welcome!


rovf
Veteran

Jan 2, 2012, 12:47 AM

Post #2 of 19 (2378 views)
Re: [karthi_perl] FTP Files from Linux to DB2 [In reply to] Can't Post

(1) Uncomment "use strict" and fix all error message, before proceeding.

(2) You have commented out the put(...) line, so no wonder that no files get uploaded.

(3) You don't check for the existence (and read permission) of your files.

(4) Just to improve readability: Inside your foreach loop, you already have the filename stored in $_, so no need to use an index into the @files array. Actually, if the names of the files are statically known, you don't need @files at all:


Code
for ('PLP301C1.TXT', 'PLP301C2.TXT', 'PLP302C1.TXT')) { 
if(-r $_ && -f $_) {
$ftp->put($_) or ....;
} else {
....
}
}


or better:


Code
for my $file ('PLP301C1.TXT', 'PLP301C2.TXT', 'PLP302C1.TXT')) { 
...
$ftp->put($$file) or ....;
...
}



rovf
Veteran

Jan 2, 2012, 1:06 AM

Post #3 of 19 (2376 views)
Re: [karthi_perl] FTP Files from Linux to DB2 [In reply to] Can't Post

Just checked the code for Net::FTP (the version which comes Perl 5.10.1, because this is what I have installed on this machine):

- It seems that the "put" method already checks for existence of the input file. If the file can be opened, a message is written to STDERR and undef is returned.

- $ftp->messaged does not necessarily contain a useful error message, if $ftp->put fails.

- As an additional diagnostics aid, I would try to read back the file after putting:



Code
... 
my $generated_filename=$ftp->put($file);
if($generated_filename) {
my $tempfile='tmp.xxx';
$ftp->get($generated_filename,$tempfile) or warn "Can not get back $generated_filename - ".$ftp->message;
unlink $tempfile;
} else {
warn "Can not put $file - ".$ftp->message;
}
...



karthi_perl
Novice

Jan 2, 2012, 1:32 AM

Post #4 of 19 (2374 views)
Re: [rovf] FTP Files from Linux to DB2 [In reply to] Can't Post

I have changed the code as below.

#!/usr/bin/perl
#use strict;
use warnings;
use Net::FTP;
use Cwd;
use File::Basename;

################################
#Main Execution begins here
################################

$ftp = Net::FTP->new('xxxr-west.xxx.com', Debug => 0) or die "Couldn't connect to: $@\n";
$ftp->login(xxx,xxx') or die "Couldn't login to PSS server\n", $ftp->message;
$ftp->pwd();
print "change directory to /FTPPLPM\n";
$ftp->cwd(' FTPPLPM' ) or die "Couldn't change directory\n", $ftp->message;

chdir("/bis_data/msg/IProp_Data_Automation/Output/PSS");
#Get PSS Files
for my $files('PLP301C1.TXT', 'PLP301C2.TXT', 'PLP302C1.TXT')
{
$ftp->put($$files)
}
$ftp->quit;


################




I am getting the below error message.



change directory to /FTPPLPM
Use of uninitialized value in substitution (s///) at /usr/lib/perl5/5.8.8/File/Basename.pm line 338.
fileparse(): need a valid pathname at /usr/lib/perl5/5.8.8/Net/FTP.pm line 718


rovf
Veteran

Jan 2, 2012, 1:42 AM

Post #5 of 19 (2370 views)
Re: [karthi_perl] FTP Files from Linux to DB2 [In reply to] Can't Post

(1) Let me repeat: You really *should* uncomment 'use strict'. It already catches some errors.

(2) The construct '$$files' was a typo of mine. It should, of course, be $files (please don't blindly copy code without understanding what it does).

(3) You should catch errors wherever possible. Your new code doesn't catch errors from chdir and put.


karthi_perl
Novice

Jan 2, 2012, 2:33 AM

Post #6 of 19 (2366 views)
Re: [rovf] FTP Files from Linux to DB2 [In reply to] Can't Post

Yes. I just rectified all the errorrs. I don't see any error message now. But...still ...i don't see the files in the destination. why?

Frown


rovf
Veteran

Jan 2, 2012, 3:02 AM

Post #7 of 19 (2365 views)
Re: [karthi_perl] FTP Files from Linux to DB2 [In reply to] Can't Post

You should post your new code, after you have made the changes.


karthi_perl
Novice

Jan 2, 2012, 4:26 AM

Post #8 of 19 (2362 views)
Re: [rovf] FTP Files from Linux to DB2 [In reply to] Can't Post

#!/usr/bin/perl
use strict;
use warnings;
use Net::FTP;
use Cwd;
use File::Basename;

################################
#Main Execution begins here
################################

$ftp = Net::FTP->new('ftpsrv-west.xxx.com', Debug => 0) or die "Couldn't connect to: $@\n";
$ftp->login('xxxxx','xxxx') or die "Couldn't login to PSS server\n", $ftp->message;
$ftp->pwd();
print "change directory to /FTPPLPM.\n";
$ftp->cwd(' FTPPLPM' ) or die "Couldn't change directory\n", $ftp->message;

chdir("/bis_data/msg/IProp_Data_Automation/Output/PSS_OUTPUT");
#Get PSS Files
for my $files('PLP301C1.TXT', 'PLP301C2.TXT', 'PLP302C1.TXT')
{
$ftp->put($files)
}
$ftp->quit;


rovf
Veteran

Jan 2, 2012, 4:55 AM

Post #9 of 19 (2360 views)
Re: [karthi_perl] FTP Files from Linux to DB2 [In reply to] Can't Post

(1) Your program, as posted, can not run (because you use an undeclared variable $ftp, which is forbidden under "use strict"). This means that, whatever you have executed, is NOT the program you have posted here.

(2) You have not incorporated in your program all suggestion from my previous responses.


karthi_perl
Novice

Jan 2, 2012, 5:26 AM

Post #10 of 19 (2358 views)
Re: [rovf] FTP Files from Linux to DB2 [In reply to] Can't Post

can you fix the issues for me? since this is my first PERL program, i an unable to catch all your points.


karthi_perl
Novice

Jan 2, 2012, 5:28 AM

Post #11 of 19 (2357 views)
Re: [karthi_perl] FTP Files from Linux to DB2 [In reply to] Can't Post

$ftp->put($files) or die "couldn't transfer the file\n", $ftp->message;


karthi_perl
Novice

Jan 2, 2012, 5:28 AM

Post #12 of 19 (2356 views)
Re: [karthi_perl] FTP Files from Linux to DB2 [In reply to] Can't Post

Global symbol "$ftp" requires explicit package name at Thompson_FTP_Put.pl line 14.
Global symbol "$ftp" requires explicit package name at Thompson_FTP_Put.pl line 15.
Global symbol "$ftp" requires explicit package name at Thompson_FTP_Put.pl line 15.
Global symbol "$ftp" requires explicit package name at Thompson_FTP_Put.pl line 16.
Global symbol "$ftp" requires explicit package name at Thompson_FTP_Put.pl line 24.
Global symbol "$ftp" requires explicit package name at Thompson_FTP_Put.pl line 24.
Global symbol "$ftp" requires explicit package name at Thompson_FTP_Put.pl line 27.


rovf
Veteran

Jan 2, 2012, 5:34 AM

Post #13 of 19 (2355 views)
Re: [karthi_perl] FTP Files from Linux to DB2 [In reply to] Can't Post

Exactly, so please first fix this.

You need declare your variables as either lexicals or globals. I suggest lexicals, i.e.


Code
my $ftp=...



karthi_perl
Novice

Jan 2, 2012, 5:40 AM

Post #14 of 19 (2352 views)
Re: [rovf] FTP Files from Linux to DB2 [In reply to] Can't Post

Bareword "global" not allowed while "strict subs" in use at PSS_FTP_Put.pl line 13.
Execution of PSS_FTP_Put.pl aborted due to compilation errors.


rovf
Veteran

Jan 2, 2012, 5:48 AM

Post #15 of 19 (2351 views)
Re: [karthi_perl] FTP Files from Linux to DB2 [In reply to] Can't Post

You should not tell us all the error messages, you should fix them.

If you need help with a certain message, first add to your program

use diagnostics;

in the beginning. If you still can't find the error, post in this forum.


karthi_perl
Novice

Jan 2, 2012, 9:32 PM

Post #16 of 19 (2341 views)
Re: [rovf] FTP Files from Linux to DB2 [In reply to] Can't Post

Starting PSS FTP PUT
Uncaught exception from user code:
couldn't transfer the file
STOR fails: FTPPLPM.PLP301C1.TXT. User not authorized.


It seems like the file which i transfer to mainframe system is not in the expected format. Can anybody tell me whether the way i trnasfer the file format is correct or not.


rovf
Veteran

Jan 3, 2012, 1:43 AM

Post #17 of 19 (2338 views)
Re: [karthi_perl] FTP Files from Linux to DB2 [In reply to] Can't Post

"User not authorized" doesn't sound like "wrong file format" for me. Seems that you don't have write permission in the target directory.

Can you upload the file by FTP from the command line?

If yes, could it be an issue with passive/active mode of FTP?


karthi_perl
Novice

Jan 3, 2012, 1:46 AM

Post #18 of 19 (2336 views)
Re: [rovf] FTP Files from Linux to DB2 [In reply to] Can't Post

yes. I have tried from command prompt. I got the same error message. As you said, the destination directory doesn't have the write permission enabled for the userid which i used in the script.


rovf
Veteran

Jan 3, 2012, 1:58 AM

Post #19 of 19 (2335 views)
Re: [karthi_perl] FTP Files from Linux to DB2 [In reply to] Can't Post

Before fiddling Net::FTP, I always try to do it manually from the command line first - such types of errors are much easier to diagnose.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives