FTP/SFTP Return Codes

 7 Replies
 2 Subscribed to this topic
 27 Subscribed to this forum
Sort:
Author
Messages
Brian
Basic Member
Posts: 7
Basic Member

    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
    Veteran Member
    Posts: 479
    Veteran Member
      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
      Veteran Member
      Posts: 1498
      Veteran Member
        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
        Basic Member
        Posts: 7
        Basic Member

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

          Brian
          Basic Member
          Posts: 7
          Basic Member
            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
            Veteran Member
            Posts: 1498
            Veteran Member
              Sorry, nothing for sftp
              Brian
              Basic Member
              Posts: 7
              Basic Member
                K - Thanks for your help!
                Jason Beard
                Veteran Member
                Posts: 124
                Veteran Member

                  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