1753819 Members
8729 Online
108805 Solutions
New Discussion юеВ

FTP Errors

 
SOLVED
Go to solution
Greg White
Frequent Advisor

FTP Errors

Hello Experts,

One of my customers FTP's a few files weekly to another location. Usually my script works fine but every now and then one of the file transfers does not complete. I can't seem to find a way to get the FTP status code into any useful form. I would like to re-transmit the file if an error occurs. Any ideas out there?

Thanks, Greg
I know how to do it in pascal.
7 REPLIES 7
Patrick Wallek
Honored Contributor

Re: FTP Errors

The way I have done this in the past is to send ALL of the output to a logfile, I think the -v option to FTP, and then look for the "Transfer complete" (or whatever the message is) and if it is not there, generate an error and you can try again.

An easier way to do this is with the NET::FTP PERL module. This apparently has all kinds of error checking built in, so if you know PERL that would probably be your best bet.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: FTP Errors

Hi Greg:

I might as well make you bad right off the bat. If you are using some silly scripting other than Perl (specifically the Net::FTP module), you are doing it the hard way. You will need to download and install the module from www.CPAN.org but after that this is really easy. I already have a script that should be very close. Modify the remote host name, login, and passwd and you are ready to rumble.

FILES="file1 file2 file3"
for F in ${FILES}
do
ftp.pl ${F}
STAT=${?}
echo "File: ${F} Result: ${STAT}"
done

you could also simply
ftp.pl ${FILES}
STAT=${?}
to do them all in one shot. 0 means all is well.

2 actually means OK to FTP but that is translated to a zero by the script. You get FTP error checking for free using the Net::FTP
module.

Now throw away your dumb shell scripts and do it the smart way.

Regards, Clay


If it ain't broke, I can fix that.
Greg White
Frequent Advisor

Re: FTP Errors

Hi Guys,

The PERL answer looks great but I am a little confused on how to install a module. Can you tell me the steps involved in loading this Net::FTP module?

Thanks, Greg
I know how to do it in pascal.
A. Clay Stephenson
Acclaimed Contributor

Re: FTP Errors

Hi Greg:

Net::FTP is actually contained in libnet. I suggest that you download and install the library.
Use this link:
http://search.cpan.org/search?dist=libnet

and click on the libnet-1.12.tar.gz link.

This will download the source. You then gunzip it in a tmp directory and then untar the files.
Go to the libnet-1.12 directory directory underneath the directory in which you did the
tar. There will be a README file to guide you through the process. The first thing you will run is perl Makefile.PL. The rest is very, very simple.

If it ain't broke, I can fix that.
Dave La Mar
Honored Contributor

Re: FTP Errors

Greg -

here is how on Perl deprived rookie does it -

ftp -n -v << endl >> /$FTP_LOG
open servername (or ip)
user username userpassword
prompt
mput RC*
quit
endl


grep 'Not connected' $FTP_LOG 1>/dev/null 2>/dev/null
echo $? | read RC
grep 'full' $FTP_LOG 1>/dev/null 2>/dev/null
echo $? | read RC1
grep 'fail' $FTP_LOG 1>/dev/null 2>/dev/null
echo $? | read RC2
grep 'Error writing' $FTP_LOG 1>/dev/null 2>/dev/null
echo $? | read RC3

if [ $RC -lt 1 -o $RC1 -lt 1 -o $RC2 -lt 1 -o $RC3 -lt 1 ]
then
do something (i.e. sleep X amount of tine and resend)
fi


Of course, hard coding the user name and password are not done. A .netrc will preserve the security. Yhis is only an example. Output to /dev/null can be condensed as well.
In any event this is roughly how we handle ftp verification and email upon failures ("then do something".

Best of luck.
Someday I will learn perl as well. Clay has some very good answers to a number of postings that require perl.

dl
"I'm not dumb. I just have a command of thoroughly useless information."
Greg White
Frequent Advisor

Re: FTP Errors

Hi guys,

I got the Net::FTP module installed and the PERL script worked great. They use anonymous FTP so all I had to do was to change the hostname and login and everything worked fine. Believe it or not, the very first time I used the script it detected an imcomplete transfer and automatically resent the file!

Dave, thanks for your suggestion but after seeing this PERL script work there is no way I'm going to change. Besides who would want to make A. Clay bad (or mad)?

I have got to learn PERL.

Thanks, Greg
I know how to do it in pascal.
A. Clay Stephenson
Acclaimed Contributor

Re: FTP Errors

Hi Greg:

I'm glad you are now a Perl convert. If you really want to be impressed by Perl, try writing a script that uses sockets. What I used to do in C now works in a scripting language and it just about as fast. I especially like the idea that I can write a sockets based client/server pair and have it work in the Windows world equally well and unchanged.

Regards, Clay


If it ain't broke, I can fix that.