January 28th, 2013, 12:26 PM
How to trap sftp upload sucess or failure in unix ksh?
Trying to upload several files to a sftp site using a shell script (KSH) and wanted to find out if all files uploaded 100% or not. Can anyone tell me how to accomplish this?
January 28th, 2013, 12:39 PM
Does your sftp program set the exit status in a meaningful way?
Most programs return 0 on success, and a variety of small positive integers representing various kinds of 'problem'.
$ echo $?
$ echo $?
'grep' for example returns 0 for matching text, 1 for no matches, 2 for missing files.
January 28th, 2013, 12:52 PM
Yes, it does give return codes.
But how would I know which file succeeded or not?
Do I need to run echo $? inside the sftp session or after it exits out of the sftp session?
Can you please give me a sample script.
January 28th, 2013, 07:35 PM
You can log process and if any errors occurred parse the log file, For example:
$ cat test_sftp.sh
sftp -b /dev/stdin -o BatchMode=yes -o IdentityFile=/export/home/user/.ssh/id_rsa -o Port=22 user@host 1>sftp.log 2>&1 <<ENDSFTP
if [[ $rc != 0 ]] then
print "***Error occurred...$rc" `date "+%Y-%m-%d-%H.%M.%S"`
print "***Successful transfer...$rc" `date "+%Y-%m-%d-%H.%M.%S"`
***Error occurred...1 2013-01-28-11.28.26
$ cat sftp.log
Connecting to host...
sftp> put file_does_not_exist_1.txt
File "file_does_not_exist_1.txt" not found.