Monday, May 30, 2011

FTP Automate shell script

#!/bin/ksh
# Function : pick a given file from a given place and send it to any FTP server, it will try to trap 
#   FTP major server codes, if success then it will 
# How To use?
# Simply put this script somewhere in your machine then make +x this file and run it by passing the proper parameters
# Before you run this script , make sure the variable FTP_fixed_log_temp_dir is set. 
# this code done by (aasaady  at yahoo dot com), Saudi Arabia
if [ $# -ne 8 ]
then
  echo "Usage: $0         "
  exit 1
fi


# read passed parameters
ip_address=$1
ip_user=$2
ip_pass=$3
ftp_mode=$4
file_dir=$5
file_name=$6
target_dir=$7
log_file=$8

#Log & temp Directory for FTP purposes (not for transfere logging)
FTP_fixed_log_temp_dir=

# touched locally files folder 
touch_folder_name=${FTP_fixed_log_temp_dir}

#logfile is the script internal logging file where  is user readable comma delemeted log (temperary log file)
ftp_logfile=${FTP_fixed_log_temp_dir}_${file_name}_internal.log

# get the file zise
file_size=$(ls -lh  ${file_dir}${file_name}| awk '{print $5} ')

#ignore signals
trap "" 1 2 3 15

#echo "FTP Connection Settings ->"
#echo "--------------------------"
#echo "ftp host     : ${ip_address}"
#echo "ftp user     : ${ip_user}"
#echo "ftp password : ${ip_pass}"
#echo "ftp mode     : ${ftp_mode}"
#echo "file_dir     : ${file_dir}"
#echo "ftp_file     : ${file_name}"
#echo "target_dir   : ${target_dir}" 
#echo "ftp logfile  : ${ftp_logfile}"
#echo "user logfile : ${log_file}"
#echo "Start Time   : $(date)"
#echo "--------------------------"


#run ftp command

ftp -n -v ${ip_address} <<-EOFtp >${ftp_logfile} 2>&1
user ${ip_user} ${ip_pass}
${ftp_mode}
prompt on
cd ${target_dir} 
lcd ${file_dir}
put ${file_name}
quit
EOFtp

#trap ftp code


f=$(more ${ftp_logfile} | grep 226 | wc -l) 
if [ $f != "0" ]
then
 echo "success FTP,${file_dir},${file_name},${file_size},${ip_address},${target_dir},$(date)" >> ${log_file}

  

 # remove the log file and other temp when finished
 rm ${ftp_logfile}_4_touch
 rm ${touch_folder_name}${file_name}

else
   f=$(more ${ftp_logfile} | grep 530 | wc -l) 
 if [ $f != "0" ]
 then
 echo "Failed FTP: login failure,${file_dir},${file_name},${file_size},${ip_address},${target_dir},$(date)" >> ${log_file}
  
else
   f=$(more ${ftp_logfile} | grep 552 | wc -l) 
 if [ $f != "0" ]
 then
 echo "Failed FTP: disk space problem,${file_dir},${file_name},${file_size},${ip_address},${target_dir},$(date)" >> ${log_file}
  
else
 echo "Failed FTP: host (connection) might have problems,${file_dir},${file_name},${file_size},${ip_address},${target_dir},$(date)" >> ${log_file}
   
     fi 
 fi
fi

# remove the log file
rm ${ftp_logfile}

# this is the return code for this script, althow if FTP not success but still the scipt considerd called successfully 
#  thanks to Emmad Kareem  who suggested this line
exit 0
 Code Explanation

500 Series: The command was not accepted and the requested action did not take place.

500 Syntax error, command unrecognized. This may include errors such as command line too long.

501 Syntax error in parameters or arguments.

502 Command not implemented.

503 Bad sequence of commands.

504 Command not implemented for that parameter.

530 Not logged in.

532 Need account for storing files.

550 Requested action not taken. File unavailable (e.g., file not found, no access).

551 Requested action aborted. Page type unknown.

552 Requested file action aborted. Exceeded storage allocation (for current directory or dataset).

553 Requested action not taken. File name not allowed.

400 Series: The command was not accepted and the requested action did 
not take place, but the error condition is temporary and the action may 
be requested again. 

421 Service not available, closing control connection.This may be a reply to any command if the service knows it must shut down.

425 Can't open data connection.

426 Connection closed; transfer aborted.

450 Requested file action not taken.

451 Requested action aborted. Local error in processing.

452 Requested action not taken. Insufficient storage space in system.File unavailable (e.g., file busy).

300 Series: The command has been accepted, but the requested action is dormant, pending receipt of further information. 

331 User name okay, need password.

332 Need account for login.

350 Requested file action pending further information

200 Series: The requested action has been successfully completed. 

200 Command okay.

202 Command not implemented, superfluous at this site.

211 System status, or system help reply.

212 Directory status.

213 File status.

214 Help message.On how to use the server or the meaning of a particular
 non-standard command. This reply is useful only to the human user.

215 NAME system type. Where NAME is an official system name from the list in the Assigned Numbers document.

220 Service ready for new user.

221 Service closing control connection.

225 Data connection open; no transfer in progress.

226 Closing data connection. Requested file action successful (for example, file transfer or file abort).

227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).

230 User logged in, proceed. Logged out if appropriate.

250 Requested file action okay, completed.

257 "PATHNAME" created.

100 Series: The requested action is being initiated, expect another reply before proceeding with a new command.

110 Restart marker reply. In this case, the text is exact and not left 
to the particular implementation; it must read: MARK yyyy = mmmm where 
yyyy is User-process data stream marker, and mmmm server's equivalent 
marker (note the spaces between markers and "="). 

120 Service ready in nnn minutes.

125 Data connection already open; transfer starting.

150 File status okay; about to open data connection.

rsync with delete option and different ssh port

How to rsync e.g PIPELINE dir from Source to Destination? #rsync -avzr   --delete-before  -e "ssh -p $portNumber"  /local...