Automate FTP process and autorestart on link failure

Hi Guys,

i have this lil challenge;

i am to implement an automated script that searches/scans a directory for files then picks and sends this files to a very remote server via an ftp link.
the challenge here is that the ftp link fails due to netwrk issues maybe;
i therefore need to develop a proactive means of detecting the present ftp session is no more active,then quit and restart a new session;
as this files are not supposed to stay inthis location for up to an hour.
someone help pls;

an idea from me - if you have netcat installed use :
nc -z server.com 21
the response should be :

then echo $? will return 0, on failure will return 1. Check the error code and proceed based on the response.
-z flag just checks for a live listener / daemon on the remote port, without sending the data. This should ensure that the connection is OK.

Sounds like you should really be thinking about replacing that ftp link with something with sane error fallback and graceful degradation.

What would you suggest as an alternative to scripted FTP here? I guess there are tools better suited to failing links but have no idea what these would be?

The customary replies would be ssh/scp and/or rsync.

i wouldn't recommend using ftp as scp/sftp are much better easier in error handling and offer much better security.

Anyway, I made a while ago an auto-ftp script, find below some hints:

  • In fact you should make your script loop untill a success condition is reached, which in this case means all files are transferred successfully.
  • In your script, you should save the ftp output and have it checked to ensure last transfer is ok. this is what I came up with:

I just grabbed this code from the source scripts, so it might not be ready for use. it is just to get the idea.

In fact if you could grep ^226 $resultfile |grep -v "226 bytes" it should be a good indication the file is transmitted successfully.
The reason to grep -v "226 bytes" is in case the file is 226 bytes you'll grep
226 bytes transferred (or something like that).

#syntax:
#analyze_ftpresult <ftpresult file>

function analyze_ftpresult
{

\# check rfc 959 on http://www.ietf.org/rfc/rfc0959.txt
\#
resultfile=$1
is_error=0
is_noconnect=\`fgrep "Not connected" $\{resultfile\} 2&gt;/dev/null\`
    is_226=\`grep ^"226 " $\{resultfile\} 2&gt;/dev/null|grep -v ^"$\{errcode\} bytes" 2&gt;/dev/null\`
    is_250=\`grep ^"250 " $\{resultfile\} 2&gt;/dev/null|grep -v ^"$\{errcode\} bytes" 2&gt;/dev/null\`

if [ -z "$\{is_226\}" -a -z "$\{is_250\}" ] ;then
            return 1
    fi
if [ -n "$\{is_noconnect\}" ] ;then
	return 10
fi
	
$\{debug\} && set \+x
\# Too much debugging info
for errcode in \`echo 421 425 426 450 451 452 500 501 502 503 504 530 550 551 552 553\`
 do
	error=\`grep ^"$\{errcode\} " $\{resultfile\} 2&gt;/dev/null|grep -v ^"$\{errcode\} bytes" 2&gt;/dev/null\`
	case $\{error\} in
		421|425|426\)	return 10 ;;
	esac
	if [ -n "$\{error\}" ] ; then
		is_error=\`expr $\{is_error\} \+ 1\`
		echo "$\{stamp\} ERROR $\{_sfile\}: $\{error\}" &gt;&gt;$\{somelogfile\}
	fi
done
$\{debug\} && set -x
return $\{is_error\}
$\{debug\} && echo "&lt;&lt; end function analyze_ftpresult &gt;&gt;"

}