Re: FTP and error codes (366 Views)
Reply
Esteemed Contributor
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 1 of 8 (366 Views)
Accepted Solution

FTP and error codes

We have a script that automatically puts files to a remote system (see below). The problem is that we have no way of telling if there was a problem. Like if the server is down or the transfer doesn't work....we still get an error code ($?) of 0 which is the same code if it works properly. Is there a way for ftp to let you know (via email) if there is a problem?

ftp -i -n
# cat /dir/input
open servername.domain
user name password
bin
ls (just using ls for testing purposes)
close
quit
~



Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 2 of 8 (366 Views)

Re: FTP and error codes

popular question....
basically, you need to "log" the output of ftp to a file and then grep for different status codes, like 250. man "ftpd" for status code lists. btw: don't check for the "verbage" as it's not standard across implementations
Honored Contributor
Ivan Ferreira
Posts: 6,957
Registered: ‎05-07-2004
Message 3 of 8 (366 Views)

Re: FTP and error codes

I use a script like this:


SEND_STATUS=`echo "user $FTP_USER $FTP_PASSWORD
bin
cd $REMOTE_USER
lcd $LOCALDIR
mput $LOCALFILE
bye" | ftp -i -v -n $FTP_HOST | grep -E "^226.*OK.$" |wc -l`
if [ $SEND_STATUS -eq 1 ]
then
echo "File transferred correctly" >> $LOGFILE
exit 0
else
echo "ERROR in ftp\n" >> $LOGFILE
echo "`date`: Error sending $LOCALFILE to user $REMOTE_USER" | mailx -s "ERROR FTP" $MAILTO
exit 1
fi
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 4 of 8 (366 Views)

Re: FTP and error codes

Hi,
if you wanted to be paranoid you could do the following steps:

1. checksum the file
2. push transfer the file
3. pull transfer the file back
4. re-calc the checksum

This would highlight any transmission problems, but at the cost of bandwidth and time !
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 5 of 8 (366 Views)

Re: FTP and error codes

Capturing FTP error codes and interpreting them (2 usually means good) is a royal pain. Use Perl's Net::FTP module and you get error checking for free.

For example, to do your example:
ftpget.pl -h servername.domain -u user -p password -B -d /etc -L 'w*'
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "ftpget failed; status ${STAT}." >&2
fi

Would log you in to a server (and can optionally read .netrc for a password), set binary mode, cd to /etc, and list all files that conform to 'w*'. Error-checking is done at every step and all you have to do is examine the exit status of the process. If it's zero, all was well. You don't even have to know any Perl. Invoke as ftpget.pl -u for full usage.

If it ain't broke, I can fix that.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 6 of 8 (366 Views)

Re: FTP and error codes

and for putting files there is a similar ftpput.pl.

If it ain't broke, I can fix that.
Honored Contributor
Bill Hassell
Posts: 14,210
Registered: ‎05-29-2000
Message 7 of 8 (366 Views)

Re: FTP and error codes

Since ftp can execute a few commands or thousands of commands, it is impossible to return a code that summarizes what happened. If you look at all the possible commands (even though your objective is to see if a file was transferred OK), you'll see that there are several steps that can fail or be successful. For instance, you might want to remove a remote file but that ftp command would fail if the file was already removed. Is that a failure or a success?

Just like ping, there is no meaningful return code about the actual objective because there are so many possibilities and only one number as a return code.

Use the Perl modules.
Honored Contributor
Rasheed Tamton
Posts: 965
Registered: ‎05-18-2002
Message 8 of 8 (366 Views)

Re: FTP and error codes

Hi,

Use with the verbose option.

Try the below one:

# cat /dir/input
/usr/bin/ftp -v -n > /var/tmp/ftpscript.log 2> /var/tmp/ftpscript.err <open servername.domain
user name password
prompt
cd /ddir
bin
mput file*
ls /tmp
quit
EOF

#sh /dir/input (run the script input)

after that re-direct /var/tmp/ftpscript.err by mail (or both the output files -log and err) to you.

Rgds.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.