Operating System - HP-UX
1833811 Members
3704 Online
110063 Solutions
New Discussion

Re: ftp command error handling

 
steven chang_1
Regular Advisor

ftp command error handling

Hi all :

I write a shell to run ftp transfering files
and I get the $? after running ftp command to
tell if anything wrong with ftp connection.
But it seems $? could not get all the
error condition.

Would you mind telling me how you deal with
the ftp command error to tell whether the
ftp transfering file or getting file running
ok.

Thank you !!
Regards,
Steven
steven
5 REPLIES 5
Antonio Cardoso_1
Trusted Contributor

Re: ftp command error handling

Steven,
I'm not sure you can achieve this using shell.
A possible way is using perl + Net::FTP + expect to control results of each individual ftp command.

regards,
antonio.
Kenan Erdey
Honored Contributor

Re: ftp command error handling

hi;

i did it like this: i directed ftp command output to a file.

ftp -n >x_mylog <<

and after command finished, i searched needed words to examine the result.
Computers have lots of memory but no imagination
steven chang_1
Regular Advisor

Re: ftp command error handling

Thank you all your response!!

It seems it's not easy to distiguish the
error from ftp command.

I take a cosideration for direct the
ftp procedure to an log file and
do regular expression to search error
condition or
writing C program or using other language
to handle file funtion.

But you know the both above costs too much.
I am a supplier , and everything change
need to evaluate the cost for my customers.

Anyway , thank you very much!!

Regards,
Steven
steven
Bill Hassell
Honored Contributor

Re: ftp command error handling

Like most network commands that connect to a remote system, the exit code only referes to the ability to connect. So if ftp fails to connect (ie, remote system is offline or unreachable) then the exit code is non-zero. What happens during the ftp session cannot be returned as an error code. The reason is that like ping, some commands work, some don't.

For instance, a CD, LCD or DIR can fail just like a PUT or GET. Suppose that 3 files are sent to the remote server but 1 fails. What would be the error code? 0.333 perhaps? And there are really two failure points for a file transfer. An error can occur on the source machine such as file not found or permission denied (to read), or on the destination machine such as permission denied (to write into a particular directory).

So you must collect all the output from the ftp session, and test each command and result for success of failure. You don't need to write a C program to do this -- standard scripting techniques will work just fine, but you'll need some experienced help writing the script.


Bill Hassell, sysadmin
A. Clay Stephenson
Acclaimed Contributor

Re: ftp command error handling

As already mentioned, Perl's Net::FTP module makes this trivially easy. Do a search of the forums for "ftpget.pl" and/or "ftpput.pl". These 2 Perl scripts do everythingthing for you and all you have to do from the shell is test $?. If it's zero then the command worked. There is even an optional automatic retry if the transfer fails.
If it ain't broke, I can fix that.