2

We have a daily process which moves file from 1 server to another via FTP using a script. Please find the snippet below:

fileTransferToDEST()
{
ftp -inv $DEST_IP 1>$2 <<END_SCRIPT
quote USER $SRV_USER
quote PASS $SRV_PASS
lcd $4
cd $3
bi
prompt
hash
mput $1
quit
END_SCRIPT
}

fileTransferToDEST $filename $logpathwithfilename $destinationpath $sourcepath

returnvalue=$?

FtpStatus=`grep "Transfer complete" $logpathwithfilename`

if [ "$FtpStatus" = '' -o "$returnvalue" != "0" ]; then

                echo;echo "FTP : Failed while transfering"
                exit 2

fi

I have been assigned to convert the FTP script to use SFTP. I have successfully finished all the necessary steps to have passwordless login in SFTP. please find the script using SFTP below:

fileTransferToDEST()
{
sftp $SRV_USER@$DEST_IP 1>$2 <<END_SCRIPT
lcd $4
cd $3
mput $1
quit
END_SCRIPT
}

fileTransferToDEST $filename $logpathwithfilename $destinationpath $sourcepath

returnvalue=$?

FtpStatus=`grep "Transfer complete" $logpathwithfilename`

if [ "$FtpStatus" = '' -o "$returnvalue" != "0" ]; then

                echo;echo "FTP : Failed while transfering"
                exit 2

fi

However I am unable to check/find how to successfully check if the file has 100% been transferred to the destination. How can I achieve this?


Code after applying -b ...Based on answer..

fileTransferToDEST()
{
echo "mput $4/$1 $3/" | sftp -b - $SRV_USER@$DEST_IP
}

fileTransferToDEST $filename $logpathwithfilename $destinationpath $sourcepath

returnvalue=$?


if [ "$returnvalue" != "0" ]; then

                echo;echo "FTP : Failed while transfering"
                exit 2

fi
  • Maybe you should use a tool like curl, see mukeshkumar.net/articles/curl/… or search for keywords "curl sftp" using your favorite search engine. See also unix.stackexchange.com/q/219419/330217 – Bodo Jun 17 at 9:59
  • @Bodo What is the plus point of using curl ? Unless you say that it is impossible to check in the logs that sftp successfully completed or there is no binary mode in sftp, there is no reason for me to go to curl.. – pOrinG Jun 17 at 10:17
  • @MartinPrikryl Done. – pOrinG Jun 17 at 10:22
  • @pOrinG I didn't know about the batch mode and didn't check if your multiple commands could be reduced. curl has a defined command line interface and you don't have to parse messages which are meant for a human user only. Compared to the batch mode with a single command only, curl does not make much difference. – Bodo Jun 17 at 11:11
2

OpenSSH sftp indicates its results using an exit code (what you are going already).

If it returns 0, everything went fine. If it returns 1, there was a problem.

No need to parse the output for an arbitrary message.

Just execute it in a batch mode, so that it aborts on any error. Use -b - switch for that (the - indicates that you still want to provide the commands using stdin, not via a file, which would normally follow the -b).

  • Done.. I have edited my question to apply as per your advise. Its working. Please let me know if I made any mistake. – pOrinG Jun 17 at 10:37
  • 1
    FtpStatus=... line should have been removed as well. – Martin Prikryl Jun 17 at 10:39
  • unix.stackexchange.com/questions/525389/… .. Question for binary mode. Thanks for your help. – pOrinG Jun 17 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.