ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

sftp error handling

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

  • sftp error handling

    We have a process that both uploads and downloads from an sftp site using qsh. Currently there is no error handling and it does mget *.* and then rm *.* on the download and then on the upload it mput *.* and then archives everything. The two issues I have seen so far is that between the mget and rm commands new files get uploaded and so we don't pull them down before deleting them and then when upload fails we archive the files without actually uploading them.

    I am not an sftp expert but I am trying to update it, first do the mget read all files we downloaded from the ifs build a custom expect script to only do a rm on the specific files.

    Secondly read through the files we need to upload, dynamically build the expect script with those files names.

    On both scripts I was planning on included custom error ids on each of the files so that if there is an issue I know how far the script got so I can clean up the files until that point below is an example just wanted to see if I was missing anything or if there are any better ideas. So far seems to work, uploads the first file without an issue and errors on the second and returns and displays 7.

    Code:
    PGM
                 DCL        VAR(&CMD) TYPE(*CHAR) LEN(500)
                 DCL        VAR(&MSGID) TYPE(*CHAR) LEN(7)
                 DCL        VAR(&MSGDTA) TYPE(*CHAR) LEN(128)
                 DCL        VAR(&EXITSTS) TYPE(*INT) LEN(4) VALUE(0)
                 DCL        VAR(&EXITCHAR) TYPE(*CHAR) LEN(4)
                 DCL        VAR(&MSG) TYPE(*CHAR) LEN(100)
    
                 CHGVAR     VAR(&CMD) +
                              VALUE('PATH=$PATH:/usr/bin:/usr/local/bin && +
                              expect -df /misjd/test.txt')
    
                 ADDENVVAR  ENVVAR(QIBM_QSH_CMD_OUTPUT) +
                              VALUE('FILE=/misjd/sftplog.txt') REPLACE(*YES)
    
                 ADDENVVAR  ENVVAR(QIBM_QSH_CMD_ESCAPE_MSG) VALUE(Y) +
                              REPLACE(*YES)
    
                 QSH        CMD(&CMD)
                 MONMSG     MSGID(QSH0000) EXEC(DO)
                    RCVMSG     MSGTYPE(*LAST) RMV(*NO) MSGDTA(&MSGDTA) +
                                 MSGID(&MSGID)
                    CHGVAR     VAR(&EXITSTS) VALUE(%BIN(&MSGDTA 1 4))
                    CHGVAR     VAR(&EXITCHAR) VALUE(&EXITSTS)
                    CHGVAR     VAR(&MSG) VALUE(&EXITCHAR *TCAT ' File transfer +
                                 failed! See /misjd/sftplog.txt')
                    SNDMSG     MSG(&MSG) TOUSR(JDAHL)
                 ENDDO
                 ENDPGM
    expect script
    Code:
     #!/usr/local/bin/expect -f                                                                         
     set timeout 20                                                                                    
     spawn sftp user@system         
     expect "user@system password: "                                                      
     send "password\n"
     expect "sftp>"                                                                                     
     send "lcd /misjd\n"
     expect "sftp>"                                                                                     
     send "cd test\n"  
     send "put testoutput1.txt\n"
     expect {
      default {exit 6}
      "100%"
     }
     send "put badfile.txt\n"
     expect {
      default {exit 7}
      "100%"
     }      
     expect "sftp>"                                                                                                                                                                                                                                                                                                                                
     send "quit\r\n"                                                                                    
     expect "$"                                                                                         
     exit
Working...
X