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

    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    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?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,403
    Rep Power
    1871
    Does your sftp program set the exit status in a meaningful way?
    Code:
    $ false
    $ echo $?
    1
    $ true
    $ echo $?
    0
    Most programs return 0 on success, and a variety of small positive integers representing various kinds of 'problem'.
    'grep' for example returns 0 for matching text, 1 for no matches, 2 for missing files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0
    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.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    You can log process and if any errors occurred parse the log file, For example:
    Code:
    $ cat test_sftp.sh
    #!/bin/ksh
    # 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
    put file_does_not_exist_1.txt
    quit
    ENDSFTP
    rc=$?
    if [[ $rc != 0 ]]	then
    	print "***Error occurred...$rc" `date "+%Y-%m-%d-%H.%M.%S"`
    else
     print "***Successful transfer...$rc" `date "+%Y-%m-%d-%H.%M.%S"`
    fi
    
    
    $ test_sftp.sh
    ***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.

IMN logo majestic logo threadwatch logo seochat tools logo