Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

testing return status from "ftp" 3

Status
Not open for further replies.

Calator

Programmer
Feb 12, 2001
262
AU
In a ksh script I have following code:

ftp -n < ${ftp_prog}
status=$?
if [ "$status" -ne 0 ]; then
...log error message code here ...
exit 1
fi;

The ftp commands are in the file "ftp_prog" and consist of sending a file.
The problem is that ftp does not appear to return an error code if it failed to transmit the file, eg if the destination path does not exist, I get an error message from FTP but my script still completes ok.

Any ideas?
 
I don't think (but stand to be corrected) that you can reliably use $? in this circumstance. I do it by writing the output of the ftp command to a file and then grepping that file for whatever output I would expect from a successful transfer. Not perfect, I guess, but it suits my need and I've never had a problem with it (famous last words!).
 
My answer to this was to send the file and then retreive it under a different name. If the retreived file matched the sent file then the transfer was successful. There might be a more elegant solution but I saw mine as foolproof.

The ftp script looked like
Code:
open remotehost
put datafile
get datafile datafile.test
bye
then
Code:
diff datafile datafile.test >/dev/null && error_routine

Columb Healy
 
We solved this in our ftp scripting by sending a file called verify.ftp immediately after sending the 'real' file, then retrieving it to a working directory. If we're able to retrieve the file, we were really connected - if it isn't there, we shoot out an email warning to the appropriate parties.
 
This is how I check FTP return codes in my scripts:

Code:
ftplog=/tmp/ftplog.$$

ftp -nv desthost < commands.txt > $ftplog

xfrcnt1=`grep "^226 " $ftplog | wc -l`
xfrcnt2=`grep "^5[0-9][0-9] " $ftplog | wc -l`

if [ $xfrcnt1 -ne $numfiles ] || [ $xfrcnt2 -ne 0 ]
then
  echo "success"
else
  echo "failed"
fi

When looking at raw ftp output, every line is prepended with a 3 digit number describing what happened (see RFC 959). A line starting with code 226 indicates a successful transfer. A line starting with a number with 5 as the first digit indicates an error of some sort. The two grep commands after the ftp make a count of how many of each of these appear in the output. If you get any 5xx codes, or if you don't get the same number of 226 codes as you do sent files, you have an error.
 
Thank you all for your valued suggestions. I tend to base my solution on Clairvoyant's code, but reversing the success and failed messages (error in your example!)
Columb, truly fail-proof solution, but costly if you deal with very large files?
Ken, yours appears the same as Clairvoyants, but no code supplied.
 
Quite correct, Calator. Great minds think alike but some can obviously be bothered to go the extra mile ;-)

Glad you got your solution.
 
Hi guys,

look what happened!! Clairvoyant's error diagnistic logic failed on the example below:

226 Transfer complete.
560 bytes sent in 0 seconds (0.55 Kbytes/s)
221

I think we need to tell grep to exclude any lines that have the pattern "bytes sent"
Any suggestions how to code that? Or alternatives? Thanks.
 
Just use grep -v:

xfrcnt2=`grep "^5[0-9][0-9] " $ftplog |grep -v 'bytes sent' wc -l`

Should exclude this from the wc -l. HTH.
 
Sorry, missed a pipe:

xfrcnt2=`grep "^5[0-9][0-9] " $ftplog |grep -v 'bytes sent' | wc -l`
 
Save as ftp.awk:
[tt]
BEGIN {
if ( ARGC != 3 )
{ print "Awk prog. needs logfile and file-count." >"/dev/tty"
files = 1
exit
}
files = ARGV[2] + 0
ARGC--
}

/bytes sent/ { next }
/^226 / { successes++ }
/^5[0-9][0-9]/ { errors++ }

END { print (successes==files && !errors) }
[/tt]
success=`awk -f ftp.awk $ftplog $numfiles`
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top