Anurag
Anurag

Reputation:

Handling FTP error codes

Most FTP clients return an exit code "0" even if an error occured during the file transfer.

I am facing a problem, where in I am checking for the error codes. But my script gets the error code number in the bytes sent and the validation fails.

I tried it like this:

if [[ egrep '^202 |^421 |^426 |^450 |^500 |^501 |^503 |^530 |^550 |^553 |^666 |^777 |^999 ' test.log ]] echo " Error in FTP !!! " else echo " FTP Successful !!!" fi

Can any one help me out how to segregate the error code from other numbers that come along with the message "byte sent" e.g "220 Bytes sent in 0.001 seconds (220 Kbytes/sec)"?

Upvotes: 0

Views: 1291

Answers (4)

MatiMax
MatiMax

Reputation: 11

Using the Perl-variant of regular expressions you can use something like this:

if [[ grep -P '^(([45][0-9][0-9] )(?-i)(?!bytes received))|\?|(\w+: )|([Nn]ot connected)' test.log ]] echo " Error in FTP !!! " else echo " FTP Successful !!!" fi

The regular expression tests for all sorts of common FTP error codes and some on the client side. It uses a look-ahead expression '(?! …)' to test for the non-existence of the literal "bytes received" which solves your requirement — and mine as well. ;-)

The expression is far from perfect and can be expanded at your needs.

Upvotes: 1

mark4o
mark4o

Reputation: 60923

Use wget or curl. Both of them support ftp, as well as http and https, and will return the desired exit status. And they are both open source as well.

Upvotes: 0

jimyi
jimyi

Reputation: 31201

When an error code is returned, does the message only contain the error code and no text after it? If so, using the end of line anchor $ would work:

if [[ egrep '^202$ |^421$ |^426$ |^450$ |^500$ |^501$ |^503$ |^530$ |^550$ |^553$ |^666$ |^777$ |^999$ ' test.log ]] echo " Error in FTP !!! " else echo " FTP Successful !!!" fi

Upvotes: 1

Svante
Svante

Reputation: 51511

I guess that you have to be a bit more specific in your pattern, i.e., take the start of the message after the error code into the pattern.

Upvotes: 0

Related Questions