One of the basic requirements in development projects is to FTP some files on a daily basis and do some tasks such as archiving etc. It is bit easy to FTP in a shell script along with the execution of the archiving tasks but it gets little complicated when it comes to capturing errors while FTP and communicate it that to the user.
With this Article, I tried to explain the options to FTP the files in a shell script and capturing the common errors through the error codes that can be encounter while doing the FTP.
The process can be done in three parts.
- FTP the files
- Collect or capture Error and logs
- Mail it to the Concern user
How to FTP files
#ftp –inv $HOSTNAME >> $LOGFILE <<END_SCRIPT quote username $USER quote pass $PASS cd $DIR mget src* bye END_SCRIPT
$HOSTNAME = host name or server name
$LOGFILE = path of the log file where the FTP detailed output will be redirected
$USER = username ($1 – accepts the input from the user as the first parameter)
$PASS = password ($2 – accepts the input from the user as the second parameter)
$DIR = path of the directory where you want to get the files from
src* = files starting with “src”
END_SCRIPT = Marks the start and end of the FTP script
-v: Verbose option forces FTP to print all data transfer statistics and responses from the remote server.
-i : Turns off interactive mode.
-n:Prevent “auto-login” upon initial connection.
Error Capturing in FTP Process
This Method requires you to hard code as many errors as you know that the FTP process can encounter but this is not a good method as you may get new errors.
grep -i “Login failed” $LOGFILE > $LOG_ERR_FILE
grep -i “Login incorrect” $LOGFILE > $LOG_ERR_FILE
grep -i “User cannot log in” $LOGFILE > $LOG_ERR_FILE
grep -i “Connection refused” $LOGFILE > $LOG_ERR_FILE
grep -i “Connection closed” $LOGFILE > $LOG_ERR_FILE
grep -i “No such file or directory” $LOGFILE > $LOG_ERR_FILE
grep -i “system cannot find the file” $LOGFILE > $LOG_ERR_FILE
grep -i “Connection timed out” $LOGFILE > $LOG_ERR_FILE
grep -i “Not connected” $LOGFILE > $LOG_ERR_FILE
grep -i “Access is Denied” $LOGFILE > $LOG_ERR_FILE
grep -i “Access Denied” $LOGFILE > $LOG_ERR_FILE
$LOGFILE = Log file used in the FTP process
$LOG_ERR_FILE = All the errors will be copied into this log file.
This is the Generic method as you just have to capture the error codes.
grep -i “450” $LOGFILE > $LOG_ERR_FILE
grep -i “530” $LOGFILE > $LOG_ERR_FILE
grep -i “550” $LOGFILE > $LOG_ERR_FILE
grep -i “552” $LOGFILE > $LOG_ERR_FILE
These are some of the common error codes that might me used in the FTP process:
ERROR CODE: ERROR MESSAGE
Series 100: Request Initiation
125: Transfer Starting
150: About to open a new connection for data transfer
Series 200 – Successful Completion
200: Command okay
211-213 : System / Directory / File Status
225: Open Data Connection, no transfer in progress
226: Closing data connection after successful command execution
230: User Login successful
Series 300: – Command okay, action on hold on further information
331: Username okay, needs password
332: Need account information (login credentials)
Series 400: – Command not accepted, will be requested again(Temp error condition)
421: Service unavailable, user/max connection limit reached
425: Cannot open data connection
426: Transfer aborted, connection closed
450: File unavailable
452: Insufficient storage space in the system.
Series 500:– Command not accepted, requested action did not take place
500/501: Syntax error
530: User not logged in
550: File not available
552: Requested operation aborted due to space issue
553: File name not allowed
Communicate the error to the user via mail
Once you are done with capturing the error codes you can mail this to the user if you encounter any such error.
Check for the size of the error log file. If the size of the error log file is larger than 0, a mail must be sent to the required users with the error captured in the error log file.
Shell Script Code:
if [ ! -s $LOG_ERR_FILE ] then mailx -s " File Transfer Process Successful" $MAIL_LIST <<ENDOFMAIL `echo " body of the message"` ENDOFMAIL else mailx -s "File Transfer Process Failed " $MAIL_LIST < $LOG_ERR_FILE fi
Source Code Description
The first line checks the size of the error log file. If it is equal to 0, then FTP was successful and the mail will be sent to the user.
If the error log file has the size greater than 0, then mail will be sent to the user with the mail body having the error codes captured in the error log file.
ENDOFMAIL = Marks the start and end of the mail body
$LOG_ERR_FILE = Error Log file with the error codes, if any captured during the FTP process.
$MAIL_LIST = Mailing list of the users you want the mail to be sent. For e.g.
“ firstname.lastname@example.org; email@example.com; firstname.lastname@example.org ”