#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    37
    Rep Power
    14

    How to trap ftp success/failure


    Hi,

    I am doing the following
    ftp -in $host << EOF_MYFTP
    user $user $password
    cd $dest_dir
    put $dest_file yourftp.fil
    EOF_MYFTP

    I want to know if really 'put' has succeeded in putting the file to destination. If for any reason the file is not put in destination [maybe due to problems in destination server like space constraint, etc] I need to know that. I just need to know if it has succededd or failed and I dont want to know
    why it din't.
    Based on the failure I will exit this prog with exit code 1 and if it is success I want to exit with exit code 0.
    Is there any way?

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,198
    Rep Power
    17
    ftp is very avaricious with exit codes, it's printing messages
    a way is collect and check the output, like:
    ftp -in $host << EOF_MYFTP >result
    user $user $password
    cd $dest_dir
    put $dest_file yourftp.fil
    EOF_MYFTP

    then using grep|sed or what ever, analize the 'result' file
    don't forget a 'bye' or 'quit' before EOF_MYFTP

    - a second way is to use a better ftp: google for 'ncftp'
    it's really better

    btw: ftp can get probls running in 'passive' mode, not ncftp.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    60
    Rep Power
    14
    Ditto guggach

    AVS,
    I've updated your other post "trap 'exit 1' 0 not working as expected" with something similar to guggach's suggestion...
    I've included it below too:

    If you "dir" the file straight after you've "put" it then you could check to see if the file exists on the remote node...

    Your code may then be something like the following (note I've not tested it!) :

    Code:
    #-- Function doftp
    doftp()
    {
    ftp -in $host << EOF_MYFTP
    user $user $password 
    put YOURFTP.FILE $dest_dir/$dest_file
    dir $dest_dir/$dest_file
    bye
    EOF_MYFTP
    }
    
    #-- Run ftp
    doftp 1>doftp.log 2>&1
    
    #-- Check logfile for "No such file or directory"
    NUM_ERRRORS=`grep -c "No such file or directory" doftp.log`
    
    if [[ ${NUM_ERRORS} -eq 0 ]]; then
      echo "File(s) transferred successfully"
    else
      echo "Errors transferring file(s):"
      grep "No such file or directory" doftp.log |awk '{print $1}'
    fi
    
    exit ${NUM_ERRORS}

    Cheers, Andy
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep Power
    0
    One way to get the successful of putting file on remote machine will be with log file there.

    We can use tee command / script command to do this.

    ftp -in $host <<EOF | tee /tmp/logfile

    user $username $passwd
    cd /tmp/dir
    put testfile .
    bye
    EOF

    so that it will contain all error / stdout shell informations on /tmp/testfile there. After completion of ftp, try to analyse results with that file there.

    On using script command then,

    script /tmp/ftplogfile
    ftp -in $hosts << EOF
    user $user $passwd
    cd /tmp/dir
    put testfile .
    bye
    EOF
    exit # Script stopping here.

    Do same grepping here.

    If you have option to use rcp / scp then it will be great there. We can directly use return as,

    rcp /tmp/testfile $host:/tmp/dir/testfile
    if [[ $? -eq 0 ]]; then
    echo "Success";
    else
    echo "Failure";
    fi

    HTH.
  8. #5
  9. No Profile Picture
    SmarSlim
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Saudi Arabia
    Posts
    8
    Rep Power
    0

    Lightbulb here what I did and it works fine


    hi,
    I came across this bad experience before , but what I did is the following

    1- use a temp log file for every file I am going to FTP
    2- log FTP behaviour in the temp file with
    3- analysis/grep the interested results you want
    4- as a bonus you can use FTP error-codes in-order to better analyze the FTP results (see next reply please)

    Code:
    #!/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 <more detailes are omitted>
    # 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 <host_name> <ftp_login>  <ftp_pass> <ftp_mode{asc|bin}> <source_dir> <source_file> <target_dir> <log_file>"
      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=<your temp directory here>
    
    # touched locally files folder 
    touch_folder_name=${FTP_fixed_log_temp_dir}
    
    #logfile is the script internal logging file where <Log_file> 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}
    
    		<do your actions here when FTP success>
    
    	# 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}
    		<do your actions here when FTP failed>
    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}
    		<do your actions here when FTP failed>
    else
    	echo "Failed FTP: host (connection) might have problems,${file_dir},${file_name},${file_size},${ip_address},${target_dir},$(date)" >> ${log_file}
    			<do your actions here when FTP failed>
    	    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
  10. #6
  11. No Profile Picture
    SmarSlim
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Saudi Arabia
    Posts
    8
    Rep Power
    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.

IMN logo majestic logo threadwatch logo seochat tools logo