Operating System - HP-UX
1822002 Members
3880 Online
109639 Solutions
New Discussion юеВ

How to capture return codes from FTP

 
SOLVED
Go to solution
KapilRaj
Honored Contributor

How to capture return codes from FTP

Guys,

I have the following script to perform automated FTP ; I need to check the return code after a file is transmitted. If I do ...

ftp>!echo $? it always gives me "0". Which means this is not an error in my current shell but the ftp shell ! How do I capture the ftp error codes ?.

Please note that this node is out side my firewall and I only have ftp port open for this ! (i.e. I can not do an remsh ls -l to check in that case i would have gone for rcp ! )

*******************
#FTP Script

if [ $# -lt 4 ]
then
echo Usage :$0 host_name user_name passwd filename
fi

echo $* |read host_name user passwd file
#ping -c1 $host_name

#if [ $? - ne 0 ]
#then
# host is not reachable
# exit 0
#fi

if [ ! -f $file ]
then
echo Unable to access file $file
fi

ftp -n << END
open $host_name
user $user $passwd
prompt off
bin
put $file
XXXXXXXXX <<<=========== Here I need to capture whether a file is transmitted properly or no !
quit
END
**********************

Regds,

Kap
Nothing is impossible
14 REPLIES 14
Mark Grant
Honored Contributor

Re: How to capture return codes from FTP

Sorry, you can't do this.

Well, you can if you use something else. For example you could use "expect" to do your "ftp" for you and "screen scrape" the output to see if there is an error. This though, is horrible.

Alternatively, use "rcp" which does have an exit code which you can test.

If you have to use "ftp", I think you'll have to do something like check the size of the file on the server and then the size of the file you have recieved.

This kind of thing might help though
#!/usr/bin/perl

use Net::FTP;

$ftp=Net::FTP->new("ftp server");
$ftp->login("user","password");
$ftp->get("file") or die "Oh dear file didn't come over properly\n";
$ftp->quit;


Never preceed any demonstration with anything more predictive than "watch this"
Hoefnix
Honored Contributor

Re: How to capture return codes from FTP

Kap,

What you can do is grep the return codes of the FTP commands.
Use the command ftp -nv << END ....

When grepping on the output you can conclud if the file went over without any errors.

Check also next thread:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=439471

HTH,
Peter
KapilRaj
Honored Contributor

Re: How to capture return codes from FTP

So I am left with the option to use perl (Planing to use Mr. A.Clay's script) BUUUT I am zero abt perl !!!

Also let me know If the following perl version is OK "5.005_03" !

Whats the syntax of Mr Clay's script ?. I would like to be a user than trying to act as a techy when I play with things on which i am not confident !

Mr A.Clay , Can u please let me know the syntax or ways to invoke this script I owuld like to keep the existing Syntax as my original shell script !

i.e. Command host_name user_name passwd file_name

Please help me out guys ..

Kaps
Nothing is impossible
Nicolas Dumeige
Esteemed Contributor

Re: How to capture return codes from FTP

ftp has its own way of displaying success or failure. It shows the completion of a task with a code starting with 2xx.
Using expect, I suppose you can deal with this return accordingly.

As for perl, look at cpam, maybe you'll find a module suiting your need.

All different, all Unix
Colin Topliss
Esteemed Contributor

Re: How to capture return codes from FTP

If you decide to use Perl, the Perl module you'll need is called libnet-1.18.tar.gz - you can find it at http://www.perl.com/CPAN/modules/01modules.index.html

I'll see if I have an example for you to use (code at home unfortunately, so it will be later). Its quite easy to use, and infinitely more stable than Expect (tried both methods - Expect always seemed to leave hanging processes everywhere). The Perl version you mentioned will work OK.

Col.
Mark Grant
Honored Contributor

Re: How to capture return codes from FTP

#!/usr/bin/perl

use Net::FTP;

$ftp=Net::FTP->new("$hostname");
$ftp->login("$user","$password");
$ftp->get("$file") or die "Oh dear file didn't come over properly\n";
$ftp->quit;
exit 0;

Save this as "myftp.pl" and then run as

myftp.pl hostname user password file

It will have an non 0 exit status on failure
Never preceed any demonstration with anything more predictive than "watch this"
KapilRaj
Honored Contributor

Re: How to capture return codes from FTP

Can't call method "login" on an undefined value at ./myftp.pl line 4.

Kaps
Nothing is impossible
Mark Grant
Honored Contributor

Re: How to capture return codes from FTP

Sorry, I'm an idiot and didn't test it. You need this as your first line after the

#!/usr/bin/perl

($hostname,$user,$password,$file)=@ARGV;
Never preceed any demonstration with anything more predictive than "watch this"
KapilRaj
Honored Contributor

Re: How to capture return codes from FTP

No u are'nt a stupid !! It's me who doesnt even know this !

Thanks it is resolved at last leme ; This case is closed !

Kaps


Nothing is impossible
KapilRaj
Honored Contributor

Re: How to capture return codes from FTP

Hello I want to award 10 points to few in this thread ; I thought I can upgrade them anytime !!! but NOOOOOOOO!

Hey how can I ?

Kaps
Nothing is impossible
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to capture return codes from FTP

You can use Perl 5.005 but you will need to download and install the Net::FTP module from www.perl.org/CPAN. It's actually easier to use Perl 5.6.1 or above which bundles Net::FTP for you.

The other thing to note that it is not necessary to supply the passwd because ythat can be read from the user's .netrc file. Man netrc for details.

I would use the Perl script that is attached. It's more advanced than most of the example scripts I have posted and is nearly the version I use in real life.
You don't need to ping beforehand as a non-zero result will be returned to the shell for whatever reason of failure.

I would use it something like this:

ftpput.pl -h remhost -l remuser -p topsecret -A -d /mydir/myfiles -t 3 myfile1 myfile2
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "All ok"
else
echo "Bad. Status = ${STAT}"
fi

This would connect to host remhost (-h remhost) as user remuser (-l remuser) with password topsecret (-p topsecret) in ASCII mode (-A) and cd to /mydir/myfiles (-d /mydir/myfiles). Each transmission would try up to 3 times (-t 3) before giving up. The last arguments are the files to send.

Again, there is no need to supply the passwd because that can be supplied from .netrc.
Invoke as ftpput.pl -u for full usage. Now all your shell scripting is trivially easy.



Here is ftpput.pl:
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to capture return codes from FTP

If you want to turn the world around, here is ftpget.pl; the arguments are exactly the same. Invoke as ftpget.pl -u for full usage.

If it ain't broke, I can fix that.
Chris Vail
Honored Contributor

Re: How to capture return codes from FTP

Everybody has their own favorite way of doing this. Here's mine: don't use ftp. Use secure copy instead, and set it up according to the instructions I've attached.
Secure copy and secure shell both return the exit code of whatever it is you executed on the remote host:
ssh $REMHOST "ls -l /etc/hots"
echo "$?"
1
ssh $REMOST "ls -l /etc/hosts"
echo "$?"
0

Further its a lot easier to script, and its very secure: scp $FILE $REMHOST:/$DIR/$FILE.
You can guarantee the intact delivery of your file with checksums:

SUM1=`sum -r $DIR/$FILE|awk '{ print $1 }'`
SUM2=`ssh $DESTHOST "sum -r $DIR/$FILE|awk '{ print $1}'"`
if test "$SUM1" -ne "$SUM2"
then
echo "There is a problem with $DIR/$FILE"
fi

Secure shell is not hard to install, and the configuration is easy. Try it, you'll like it.



Chris
Greg OBarr
Regular Advisor

Re: How to capture return codes from FTP


You CAN use $? to get information about if FTP itself failed. For example, if your /var filesystem is full, ftp will fail (it needs /var/temp to store temporary files in order to work properly). If you don't check the $?, you may not detect this failure because FTP may not return an error to stdout.