Capturing failed FTP error

Hi All,

Please check the below ftp related job, which is deleting the files from remote host. Problem is it is not capturing the ftp failure error, and the exit status is still '0' eventhough the deletable files are not present in remote location OR ftp credential are incorrect.

#!/usr/bin/ksh -x

export REMOTEHOST1=10.19.5.221
export REMOTEUSER1='ganap_01'
export FTP_PASSWD='mysore001'
export OUTPUT_DIR='/export/home/ganap'

ftp -n $REMOTEHOST1 <<EOD
user "$REMOTEUSER1" "$FTP_PASSWD"
del $OUTPUT_DIR/aaa.log
del $OUTPUT_DIR/bbb.log
del $OUTPUT_DIR/ccc.log
del $OUTPUT_DIR/ddd.log
bye
EOD

# Checking the success of the FTP call
EXIT_STATUS=$?
if [ EXIT_STATUS -ne 0 ];then
echo FTP Failed
exit 1
fi

Please see the result below:

/export/home/ITC_rlok>ftp*
+ export REMOTEHOST1=10.19.5.221
+ export REMOTEUSER1=ganap_01
+ export FTP_PASSWD=mysore001
+ export OUTPUT_DIR=/export/home/ganap/
+ ftp -n 10.19.5.221
+ 0<<
user "ganap_01" "mysore001"
del /export/home/ganap/aaa.log
del /export/home/ganap/bbb.log
del /export/home/ganap/ccc.log
del /export/home/ganap/ddd.log
bye
/export/home/ganap/aaa.log: No such file or directory.
/export/home/ganap/bbb.log: No such file or directory.
/export/home/ganap/ccc.log: No such file or directory.
/export/home/ganap/ddd.log: No such file or directory.
+ EXIT_STATUS=0
+ [ EXIT_STATUS -ne 0 ]

I dont know, how to handle this. Please suggest/guide me.

With Regards,
Mysore Ganapati. :stuck_out_tongue:

thats the return status of the ftp session established,
since it had completed successfully, the return status is 0

ftp session return status overrides the return status of the commands executed within the ftp session.

To acheive that, possible way could be redirect the ftp logs ( commands issued, output received from the ftp server )
once the ftp session is done,
parse the log,
to identify the operation was successfull or not
ftp return codes are available and with that it should be easier to identify the status of the operation

Thanks for reply matrixmadan,

Then, what is the use of checking the exit status of the ftp command?
Can I skip the below testing block? When this can be used? Are there any other way to exit this condition with the exit status '1'? :confused:

EXIT_STATUS=$?
if [ EXIT_STATUS -ne 0 ];then
echo FTP Failed
exit 1
fi

Cheers~~
Ganapati.

Also Iam not getting any log file for this ftp process.
Where can I find this log file or how do I create this?

Can any one help me. :confused:

Thanks in advance,
Mysore Ganapati.

you can put >> logfile at :

...
ftp -n $REMOTEHOST1 <<EOD >> logfile.txt
...
EOD

for more verbose output, you can put -d (debugging) if your ftp client supports debugging mode
eg

...
ftp -d -n $REMOTEHOST1 <<EOD >> logfile.txt
...
EOD
  1. Use different client.

  2. Use something like this (change the process substitution with pipe, if your shell doesn't support it):

ftp -n your_host < <(printf "%s\n" "user your_user your_pass" "get your_file" "quit")>err;\
[ -s err ]&&exit 1