Home » Infrastructure » Unix » ftp Return Code
ftp Return Code [message #247544] Tue, 26 June 2007 05:51 Go to next message
Imran_Chennai
Messages: 22
Registered: June 2007
Location: CHN,INDIA
Junior Member

Hi,

Can any 1 pls say how to get the ftp return code,

I want to call a procedure based on the ftp (success/failure)

In my case ftp returns 0 in though it fails.

FYI my code is
#-------------------------
ftp -i -n -v $HOST >> ${LOGFILE} << EOF
user ... ...
ascii
lcd ${OUTBOUND_DIR}
put ${src_fname}
quit
EOF
#-------------------------


Thanx in advance
Imran | Chennai | India
Re: ftp Return Code [message #247610 is a reply to message #247544] Tue, 26 June 2007 09:44 Go to previous messageGo to next message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
It's in $? just after the command finishes, so you can there assign it to another variable.

Example :

ftp -i -n -v $HOST >> ${LOGFILE} << EOF
user ... ...
ascii
lcd ${OUTBOUND_DIR}
put ${src_fname}
quit
EOF
retcode=$?

echo "Exitcode FTP  :${retcode}"


Re: ftp Return Code [message #247614 is a reply to message #247610] Tue, 26 June 2007 09:50 Go to previous messageGo to next message
Imran_Chennai
Messages: 22
Registered: June 2007
Location: CHN,INDIA
Junior Member

Hi,

I gave the same but it returns 0 for both cases, later i come to know even for manual ftp it returns 0

ftp -i -n -v $HOST >> ${LOGFILE} << EOF
user ... ...
ascii
lcd ${OUTBOUND_DIR}
put ${src_fname}
quit
EOF
RC=$?
echo "Status of ftp " $RC

if [[ ${RC} -ne 0 ]]
then
echo "FTP did not complete successfully, RC=${RC}" >> ${FTP_LOGFILE}
exit 50
else
echo "FTP completed successfully, RC=${RC}" >> ${FTP_LOGFILE}
exit 0
fi
Re: ftp Return Code [message #247621 is a reply to message #247614] Tue, 26 June 2007 10:07 Go to previous messageGo to next message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
Of course you get the return code of the "ftp" command itself.

Which means it could be that the ftp command returns a 0 even when there were errors in the script.

If you want to check every FTP command for errors, you might want to consider using PERL and Net::FTP instead of a normal FTP client.

http://www.unix.org.ua/orelly/perl/perlnut/ch16_02.htm
Re: ftp Return Code [message #247623 is a reply to message #247621] Tue, 26 June 2007 10:14 Go to previous messageGo to next message
Imran_Chennai
Messages: 22
Registered: June 2007
Location: CHN,INDIA
Junior Member

I dont know Perl, i have to complete this using unix / oracle resource only


I just need the return code, based on that am calling a procedure with this as IN parameter.

So we no need to bother abt exact return code , my code will consider only 0 and non 0.


Thanks
Imran
Re: ftp Return Code [message #247905 is a reply to message #247623] Wed, 27 June 2007 08:31 Go to previous messageGo to next message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
I just need the return code


Well, when the FTP program itself doesn't provide a > 0 return code for errors during the transfer, your're out of luck in that regard.
Re: ftp Return Code [message #248026 is a reply to message #247905] Wed, 27 June 2007 16:47 Go to previous messageGo to next message
andrew again
Messages: 2577
Registered: March 2000
Senior Member
This is from an old post I no longer find on the web...

--===================================================
Does anyone have an example of a UNIX ftp script that verifies the file was completly received from the remote machine?
--===================================================
After spending most of the day trying an elegant solution, I settled on this one. This is a section of the code.
echo "ftp -n -v << *EOI" > $SCRIPT/ivr_call_detail_ftp.ksh
echo "open 172.21.102.231" >> $SCRIPT/ivr_call_detail_ftp.ksh
echo "user peri peri;" >> $SCRIPT/ivr_call_detail_ftp.ksh
echo "ascii" >> $SCRIPT/ivr_call_detail_ftp.ksh
echo "cd DOWrptfiles" >> $SCRIPT/ivr_call_detail_ftp.ksh
echo "ls -ltr" >> $SCRIPT/ivr_call_detail_ftp.ksh
echo "get $CALL_FILE ivr_call_detail_$DATE_STAMP.dat" >> $SCRIPT/ivr_call_detail_ftp.ksh
echo "bye" >> $SCRIPT/ivr_call_detail_ftp.ksh
echo "*EOI" >> $SCRIPT/ivr_call_detail_ftp.ksh

chmod 750 $SCRIPT/ivr_call_detail_ftp.ksh

$SCRIPT/ivr_call_detail_ftp.ksh > $LOG/ivr_call_detail_ftp.log

#Get the row from the ls -ltr command that
#contains the remote file and awk for the size.
#Peri is the owner of the file. CALL_FILE is 
# the name of the remote file.
RMT_FILE_SIZE=`awk '($3=="peri") && ($9 == "$CALL_FILE") {print $5}' $LOG/ivr_call_detail_ftp.log`

echo "Remote File Size: $RMT_FILE_SIZE."

#Get the row from the ls -ltr command that 
#contains the local file and awk for the size.
LCL_FILE_SIZE=`ls -ltr $DATA/ivr_call_detail_$DATE_STAMP.dat | awk '{print $5}'`

echo "Local File Size: $LCL_FILE_SIZE."

if (($LCL_FILE_SIZE == $RMT_FILE_SIZE))
then
echo "FTP Successful."
else
echo "FTP Failed."
fi
Re: ftp Return Code [message #248315 is a reply to message #248026] Thu, 28 June 2007 11:05 Go to previous message
Imran_Chennai
Messages: 22
Registered: June 2007
Location: CHN,INDIA
Junior Member

Fantastic Andrew...

The logic is great...

Due to lack of time i did it with another concept

copying FYI...

ftp_result1=`grep -c '226 transfer complete' ${LOG_FILE}`
ftp_result2=`grep -c '250 Transfer complete' ${LOG_FILE}`
ftp_result3=`grep -c '530 Login incorrect' ${LOG_FILE}`
ftp_result4=`grep -c 'No such file or directory' ${LOG_FILE}`
ftp_result5=`grep -c '226 Transfer complete' ${LOG_FILE}`

if (( ftp_result3 > 0 ))
then
print -u1 "FTP failed on login \n"
exit -1
fi

if (( ftp_result4 > 0 ))
then
print -u1 "File not found to FTP \n"
exit 0
fi

if (( ftp_result1 == 0 && ftp_result2 == 0 && ftp_result5 == 0 ))
then
print -u1 "FTP failed \n"
exit -1
fi


Thanks
Imran
Previous Topic: FTP put /send file transfer
Next Topic: Migration from oracle 8i IBM Z/OS mainframe to 10gR2 on IBM AIX
Goto Forum:
  


Current Time: Mon Apr 15 23:57:22 CDT 2024