PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 12/05/2018 12:42 PM by  Jason Beard
FTP/SFTP Return Codes
 7 Replies
Sort:
You are not authorized to post a reply.
Author Messages
Brian
Sr. Application Analyst
Private
Basic Member
(10 points)
Basic Member
Posts:4


Send Message:

--
12/03/2018 2:45 PM

    I am using AIX/Unix that is making two FTP calls one a simple FTP the other a Secure FTP (SFTP).

    My script needs to be able to be able to determine if the FTP was successful or not.  Does anyone have any ideas or examples on how I could try this?  Right now, I'm just trying to test the connection by pinging the server.  Once I get it to work, I'll expand it to use an FTP command.  This is what I have so far

     

    STATUS=`ping -v -c 1 servername`

    if [ "$(echo $STATUS | grep -c "NOT FOUND")" -ge 1 ]
     then
       echo "No Connection"
     else
       echo "Connection successful"

    It works fine if I use the correct server name but if I alter the name to test the "NOT FOUND" condition, I still get a "Connection successful" message.

    Kwane McNeal
    Private
    Private
    Veteran Member
    (1275 points)
    Veteran Member
    Posts:425


    Send Message:

    --
    12/03/2018 2:57 PM
    Brian,
    This issue is you’re testing the return code status of the echo against the 1. Echo will ALWAYS succeed, as it will ALWAYS return zero

    You’ll need to change the grep to do the compare of the string directly, not check an return code

    Greg Moeller
    Private
    Private
    Veteran Member
    (3921 points)
    Veteran Member
    Posts:1393


    Send Message:

    --
    12/03/2018 2:59 PM
    I've got a working example of the ftp command..

    if [ -s ${SRCFILE} ]; then

    mv ${SRCFILE} ${WRKFILE}
    cat ${WRKFILE}|tr [a-z] [A-Z] > ${SRCFILE}

    RESULT=`ftp -inv < open ftp.domain.com
    user user_id password
    ascii
    put ${SRCFILE} ${TRGFILE}
    bye
    EndFTP`

    echo "${RESULT}" >> ${TMPFILE}


    Then check the resulting variable like below:
    grep -i "226 Transfer complete" ${TMPFILE} > /dev/null
    if [ $? -eq "0" ]; then
    grep -i "bytes sent" ${TMPFILE} > /dev/null
    if [ $? -eq "0" ]; then
    STAT="0"
    fi
    fi

    else
    echo "FTP: ${SRCFILE} does not exist." >> ${TMPFILE}
    fi
    Brian
    Sr. Application Analyst
    Private
    Basic Member
    (10 points)
    Basic Member
    Posts:4


    Send Message:

    --
    12/04/2018 9:22 AM

    Thanks a lot!  I'll see if I can get a version of this working today.  I appreciate your assistance!

    Brian
    Sr. Application Analyst
    Private
    Basic Member
    (10 points)
    Basic Member
    Posts:4


    Send Message:

    --
    12/05/2018 10:12 AM
    I was able to get that to work. Thanks a lot for your help Greg! NOW, do you have something similar for SFTP? This doesn't seem to work for that.
    Greg Moeller
    Private
    Private
    Veteran Member
    (3921 points)
    Veteran Member
    Posts:1393


    Send Message:

    --
    12/05/2018 10:16 AM
    Sorry, nothing for sftp
    Brian
    Sr. Application Analyst
    Private
    Basic Member
    (10 points)
    Basic Member
    Posts:4


    Send Message:

    --
    12/05/2018 10:45 AM
    K - Thanks for your help!
    Jason Beard
    Private
    Private
    Veteran Member
    (306 points)
    Veteran Member
    Posts:108


    Send Message:

    --
    12/05/2018 12:42 PM

    Here's an excerpt of the pertinent pieces of an sftp script

    sftp -i ${ftpKey} -b - ${ftpUserID}@${ftpHost} <

            lcd ${LPath}

            cd ${RPath}

            get ${RFile} ${LFile}

            rm ${RFile}

            bye

    EOF

     

    sftpRC=$?

    echo ' '

    echo '     SFTP Return Code:  ' $sftpRC

    echo ' '

     

    if [ $sftpRC == 0 ]; then

        echo '     SFTP Successful'

            echo ' '

    else

        echo '     Error - SFTP Failed!'

            echo ' '

        exit 9

    fi


    Jason Beard
    617-548-5568
    jabeard3@gmail.com
    You are not authorized to post a reply.