1751707 Members
5126 Online
108781 Solutions
New Discussion юеВ

Re: FTP and error codes

 
SOLVED
Go to solution
Coolmar
Esteemed Contributor

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
~



7 REPLIES 7
OldSchool
Honored Contributor

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
Ivan Ferreira
Honored Contributor

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?
Peter Godron
Honored Contributor

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 !
A. Clay Stephenson
Acclaimed Contributor

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.
A. Clay Stephenson
Acclaimed Contributor

Re: FTP and error codes

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

If it ain't broke, I can fix that.
Bill Hassell
Honored Contributor

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.


Bill Hassell, sysadmin
Rasheed Tamton
Honored Contributor
Solution

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.