1823725 Members
3353 Online
109664 Solutions
New Discussion юеВ

ftp error trapping

 
SOLVED
Go to solution
Andrew Briggs_1
New Member

ftp error trapping

I have a shell script that performs an interactive ftp session.

How can I trap errors if the ftp aborts due to the network going down or the local disk/volume fills up ?

Any help appreciated

Thanks

Andy
10 REPLIES 10
Stefan Farrelly
Honored Contributor
Solution

Re: ftp error trapping

We use the -v option on ftp so we can verbose output then direct all output and errors from the ftp script to a file, then grep for certain errors in that, eg;

ftp -v </tmp/tempfile 2>&1


EOF

Then check the contents of /tmp/tempfile to see if it worked or any errors.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Andrew Briggs_1
New Member

Re: ftp error trapping

Stefan,

Thanks for that, I'll give it go and let you know.

Do you know if there a list of all the verbose messages for the different errors that may occur. ?

Andy
Chris Wilshaw
Honored Contributor

Re: ftp error trapping

FTP works using 3-digit error codes, which are normally displayed on the screen (or to a log)

ftp server1
Connected to server1
220 server1 FTP server
Name (server1:chris):
331 Password required for chris.
Password:
230 User chris logged in.

Where

1st digit

1 FTP request initiated ... waiting for reply ACKNOWLEDGE
2 FTP action completed ... waiting for new REQUEST
3 FTP command accepted ... waiting for more INFORMATION
4 FTP command rejected ... waiting for command RE-ISSUE
5 FTP command rejected ... waiting for new REQUEST

second digit

1 FTP in reply to request for info
2 FTP requested info on control and data connection
network or ftp server is out of operation
3 FTP invalid reply to login or accounting authentication
login or netrc not setup or has been altered
4 FTP code 4 not used
5 FTP requested info on server file system status


3rd digit - further qualifier for 2nd digit (don't have details for this but mostly 0).


So, for example you fail to login, you receive

530 Login incorrect.

and if your network connection drops (or is killed)

421 Service not available, remote server has closed connection

Normally, the best way to trap these errors is (as Stefan said) to log the connection details, and to use the log for error checking.

HTH.

Chris
Stefan Farrelly
Honored Contributor

Re: ftp error trapping


Andy,

I dont think there is a master list of ftp errors - so many different things could cause different errors from;
invalid username/password
cant connect to server
ftp simply hangs - eventually timesout (network problem or destination server hung)
remote directory full
permission error on source file or writing to destination
filename already exists on remote
cant change to dir
etc etc.
You could either create some of these events yourself to see the error message generated or run if for a while and monitor and you will slowly pickup all the major types of errors you encounter.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Patrick Wallek
Honored Contributor

Re: ftp error trapping

If you know Perl, there is a PERL::FTP module that will do stuff like this as well.

I don't know Perl so I can't give any real details, but this would be an option for you as well.
Chris Wilshaw
Honored Contributor

Re: ftp error trapping

I forgot to mention, the ftpd man page contains more details on the error codes (this does not list the 3rd digits, although in most cases, these are not necessary, and the cause of the error can be found from the first 2).

chris
Andrew Briggs_1
New Member

Re: ftp error trapping

Thanks for all the help.

I was logging the output and "grepping" already but wanted to know if there was a more elegant and robust way of trapping the error. You've all confirmed what I suspected. Chris's help re: the breakdown of the error messages is very helpful. I didn't think to man on ftpd but only looked at ftp. I'd guessed that a Perl implementation of ftp would be better but I cannot use that as an option.

I've tried to emulate all the failures during tests so I can capture the output and then use this in my script to check for errors.

Many Thanks to you all

Andy
nanthakishore
Occasional Advisor

Re: ftp error trapping

ck=`cksum $FILENAME`
before_ftp_cksum=`echo $ck | awk ' { print $1 }'`

ftp -nv ${FTPSITE}>> $LOGFILE <user xxxxx yyyyy
prompt
cd $PROCDIR
mput $FILENAME
EndFTP

ck=`rsh -l xxxxx $FTPSITE cksum $PROCDIR/$FILENAME`
after_ftp_cksum=`echo $ck | awk ' { print $1 }'`

if [ $before_ftp_cksum -eq $after_ftp_cksum ]
then
echo "files moved to $FTPSITE status:$?" >>$LOGFILE
else
echo "Problem in FTP status:$?" >>$LOGFILE

I hope this script will help you out.
Andrew Briggs_1
New Member

Re: ftp error trapping

Thanks for the reply.

I realise that a checksum at both ends would provide the best validation of a successful transmission but my remote systems are not just HP-UX. They are Mainframe OS/390, NT, Windows 2000 all of which are not running any Unix shell environment.

Andy
nanthakishore
Occasional Advisor

Re: ftp error trapping

How about this one ..,

ftp
.....
......
EndFTP

echo "Ftp Status :$?"


Will it help you out?