ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

SFTP file transfer acknowledge

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SFTP file transfer acknowledge

    Dear All,

    I am able to transfer the file from AS/400 server to Unixbox using SFTP file transfer mechanism. Could you please tell me how we know whether the file transferred successfully or not ? IS there any thing we can find out using CL?

    Thanks in Advance!

    Regards,
    Sudha
    The Secret Of Achieving Your Goals Is Revealed Before Your Eyes.
    System Engineer

    Sudha...

  • #2
    Re: SFTP file transfer acknowledge

    Someone gave me this a while back... &CMD = my SFTP command string. I'm sure you coudl capture the MSGID and do something with it.
    Code:
    OVRPRTF    FILE(OUTPUT) TOFILE(QPRINT)                   
    STRQSH     CMD(&CMD)                                     
                                                             
    RCVMSG     MSGTYPE(*COMP) MSGDTA(&MSGDTA) MSGID(&MSGID)  
    IF         COND(&MSGID *NE 'QSH0005' *OR %BIN(&MSGDTA) + 
                 *NE 0) THEN(DO)                             
    SNDPGMMSG  MSGID(CPF9897) MSGF(QCPFMSG) MSGDTA('SFTP +   
                 script failed!') MSGTYPE(*ESCAPE)           
    ENDDO

    Comment


    • #3
      Re: SFTP file transfer acknowledge

      The way that SFTP works is that it (meaning SFTP) checks that each command runs successfully. If something fails, it aborts the script and returns a non-zero exit status.

      Therefore, to know whether the entire script succeeded or failed, all you have to do is check the exit status. I think the method that gwilburn has posted it work, though it's not the method I'd choose. I prefer to do it like this:

      Code:
      PGM
         DCL VAR(&USER) TYPE(*CHAR) LEN(10) +
             VALUE('klemscot')
         DCL VAR(&HOST) TYPE(*CHAR) LEN(100) +
             VALUE('buddy.example.com')
         DCL VAR(&CMD) TYPE(*CHAR) LEN(500)
      
         ADDENVVAR ENVVAR(SFTP_USER) VALUE(&USER) REPLACE(*YES)
         ADDENVVAR ENVVAR(SFTP_HOST) VALUE(&HOST) REPLACE(*YES)
      
         CHGVAR VAR(&CMD) VALUE('PATH=$PATH:/QOpenSys/usr/bin && +
                                 sftp -b /tmp/example.sftp $SFTP_USER@$SFTP_HOST')
      
         ADDENVVAR ENVVAR(QIBM_QSH_CMD_OUTPUT) +
                   VALUE('FILEAPPEND=/tmp/sftplog.txt') +
                   REPLACE(*YES)
      
         ADDENVVAR ENVVAR(QIBM_QSH_CMD_ESCAPE_MSG) VALUE(Y) +
                   REPLACE(*YES)
      
         QSH CMD(&CMD)
         MONMSG MSGID(QSH0000) EXEC(DO)
            SNDMSG MSG('File transfer failed! See /tmp/sftplog.txt') +
                   TOUSR(KLEMSCOT)
         ENDDO
      ENDPGM
      The QIBM_CMD_OUTPUT envvar is better than using OVRPRTF because it can append the file each time, which means you don't lose information. It also works better with multithreaded applications, becuase the ILE STDOUT does not work right in multithreaded apps.

      The QIBM_QSH_CMD_ESCAPE_MSG envvar tells QShell to send an escape message when the exit status is nonzero. This allows you to use MONMSG to capture a failure, which is somewhat easier than using RCVMSG.

      Anyway, if the above CL doesn't receive an error, it means the file transfer was successful. If it does receive an error, the details will be at the end of the sftplog.txt file in the IFS.

      Comment


      • #4
        Re: SFTP file transfer acknowledge

        Hi Both,
        Thanks!!

        I have referred the file '/tmp/sftplog.txt' , but it is having huge data like policy and other stuff. Only two lines are related to our file name and the command which we used for SFTP.

        Here we are trying to capture the log , say suppose if I transfer 120 files then I need to know what are the files got transffered and with time stamp?
        2. I have seen the DSPJOBLOG after using the SFTP command ,it is showing the job status 0 for successful and 1 for failure. This both value how I can capture in my cl program?

        Thanks in Advance

        Regards
        Sudha
        The Secret Of Achieving Your Goals Is Revealed Before Your Eyes.
        System Engineer

        Sudha...

        Comment


        • #5
          Re: SFTP file transfer acknowledge

          Originally posted by sudha View Post
          I have referred the file '/tmp/sftplog.txt' , but it is having huge data like policy and other stuff. Only two lines are related to our file name and the command which we used for SFTP.
          Sorry, I don't understand what is meant by "huge data like policy and other stuff"?

          Originally posted by sudha View Post
          Here we are trying to capture the log , say suppose if I transfer 120 files then I need to know what are the files got transffered and with time stamp?
          If you transfer 120 files, then you've successfully transferred 120 files. If file #12 of 120 should fail, then the script will abort, and the other 108 will not transfer at all. And the result will be that sftp returns a non-zero exit status to it's caller -- which (if you're uisng my CL program methodolody) will cause the MONMSG to execute, and a message sent to someone who can then look at the log to see what went wrong.

          SFTP is NOT like the IBM FTP client. The IBM client will ignore all errors and continue as if nothing went wrong. SFTP does not work this way. If SFTP sees an error, it will stop the script and tell your program that an error occurred by setting the exit status.

          Originally posted by sudha View Post
          2. I have seen the DSPJOBLOG after using the SFTP command ,it is showing the job status 0 for successful and 1 for failure. This both value how I can capture in my cl program?
          Yes, 0 means success. However, anything else besides 0 should mean failure -- please don't assume that it'd be a 1.

          Both gwilburn and I showed ways of checking this value. (We showed different ways.) gwilburn checks it directly by retrieving the message and extracting the number from it. My example uses the QIBM_QSH_CMD_ESCAPE_MSG to tell QShell to send a message we can capture with MONMSG if the value is not zero.

          Comment


          • #6
            Re: SFTP file transfer acknowledge

            Hi Scott,

            Thanks for your reply!

            Could you please advise me how to take the value in CL program for (0 or 1) in my cl progam. Or could you please advise how to retrieve messages from sndpgmmsg?

            QSH CMD(&CMD)
            MONMSG MSGID(QSH0000) EXEC(DO)
            SNDMSG MSG('File transfer failed! See /tmp/sftplog.txt') +
            TOUSR(KLEMSCOT)
            ENDDO
            Last edited by gcraill; May 6, 2013, 11:22 PM.
            The Secret Of Achieving Your Goals Is Revealed Before Your Eyes.
            System Engineer

            Sudha...

            Comment


            • #7
              Re: SFTP file transfer acknowledge

              This is off the top of my head, and is untested... but the exit status is included in the message data of the QSH0005 message that should be sent to you as an exception. So if you really need that exit status, you'd just retrieve the message (similar to what Greg did in his example, aside from the fact that it's now an exception instead of a completion message.)

              Code:
              DCL VAR(&MSGDTA) TYPE(*CHAR) LEN(4)
              DCL VAR(&EXITSTATUS) TYPE(*INT) LEN(4)
              
                .
                .
              
              QSH CMD(&CMD)
              MONMSG MSGID(QSH0000) EXEC(DO)
                 CHGVAR VAR(&EXITSTATUS) VALUE(-1)
                 RCVMSG MSGTYPE(*EXCP) MSGDTA(&MSGDTA) MSGID(&MSGID)
                 IF (&MSGID *EQ 'QSH0005') DO
                    CHGVAR VAR(&EXITSTATUS) VALUE(%BIN(&MSGDTA))
                 ENDDO
                 SNDMSG MSG('File transfer failed! See /tmp/sftplog.txt') +
                        TOUSR(KLEMSCOT)
              ENDDO
              But, I don't see what the exit status is useful for. Is it that you don't trust the QIBM_QSH_CMD_ESCAPE_MSG feature? What on earth are you going to do with it? You're going to say... "The system told me the exit status is non-zero, and look, I retrieved it and it really is non-zero!" Why bother?

              Comment

              Working...
              X