1831565 Members
3980 Online
110025 Solutions
New Discussion

checking ftp return code

 
SOLVED
Go to solution
andi_1
Frequent Advisor

checking ftp return code

Hi guys,

I am using the following to retrieve files from an ftp server:

ftp -i -n $HOSTNAME <user userID userPasswd
bin
get $SOFTWARE
EOF

How can I check if the file was successfuly retrieved or check ftp return code?

Thank you!
4 REPLIES 4
PIYUSH D. PATEL
Honored Contributor
Solution

Re: checking ftp return code

A. Clay Stephenson
Acclaimed Contributor

Re: checking ftp return code

Hi:

In the past, I used to do this stuff in the shell but now I NEVER do because I have found a much cleaner way to do it that makes error trapping duck soup. Do this stuff in Perl using the Net::FTP module which is available from http://www.cpan.org .

Here's how simple it can be:

#!/usr/bin/perl -w

use Net::FTP;
use strict;

my $ftp = Net::FTP->new("remotehost",Debug => 0);
$ftp->login("cstephen","top_secret");
$ftp->cwd("/tmp");
$ftp->get("myfile");
my $stat = $ftp->status;
my $full_stat = $ftp->code;
# $stat contains the first digit; usually all
# that you need to do is test if it is equal
# to 2. $full_stat contains the full 3-digit
# value but is seldom needed
printf("Status: %d Full Status: %d\n",$stat,$full_stat);
# Sample Test
if ($stat == 2)
{
print "Get was good\n";
}
else
{
print "Get was bad\n";
}
$ftp->quit;

I think if you start using this module you will never go back to shell scripting FTP. Moreover, these same scripts will run in the NT world. You can download a free version of perl for windows at http://www.activeperl.com.
Note that the answer is now the same on UNIX and NT and on the same subnet or across the net.


Notice that this method easily handles the error checking. If you like, you can use the shell for most of your script and simply use a bit a perl for the actual FTP transfers. In that case add the statement exit($stat) to the perl script and then your shell script does have a valid status indication.

Regards, Clay
If it ain't broke, I can fix that.
Mark Greene_1
Honored Contributor

Re: checking ftp return code

you cannot just check $?, because that will only tell you if the ftp binary exited successfully, not if there were problems with the file.

You can either go with Clay's solution, or route the output from the ftp process to a file, and then grep the file for the return codes and key error messages. I've posted a script that does this previously, I'll see if I can find the URL for that thread.

HTH
mark
the future will be a lot like now, only later
RICK TAN JEE HOW_1
Occasional Contributor

Re: checking ftp return code



1. compute a checksum for the transfered file (command cksum) file_name before the transfer and put it into a file cksum_file_name
2. transfer a file_name
3. transfer a cksum_file_name
4. compute a checksum for an already transfered file
5. compar the new checksum and the checksum in a cksum_file_name

If the checksums are identical you are sure the file was not corrupted during the transfer.

Hope it helps