SAP ABAP UNIX

From SapWiki

Ejecutar comando de sistema operativo

(Los comandos se definen en la tran. SM69) Función SXPG_COMMAND_EXECUTE

ejemplo:

FORM mueve_ficheros USING p_file p_dir p_dire CHANGING p_subrc.

    DATA: BEGIN OF lt_btcxpm OCCURS 0.
        INCLUDE STRUCTURE btcxpm.
    DATA: END OF lt_btcxpm.
    DATA: l_para LIKE sxpgcolist-parameters,
          l_aux_dir1(200),
          l_aux_dir2(200),
          l_string TYPE string.

    CONCATENATE p_dir   '/' p_file INTO l_aux_dir1.
    CONCATENATE  p_dire '/' p_file INTO l_aux_dir2.
    CONCATENATE l_aux_dir1 l_aux_dir2 INTO l_para SEPARATED BY space.

    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.

    CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
      EXPORTING
        commandname                   = 'ZMV'
        additional_parameters         = l_para
      TABLES
        exec_protocol                 = lt_btcxpm
      EXCEPTIONS
        no_permission                 = 1
        command_not_found             = 2
        parameters_too_long           = 3
        security_risk                 = 4
        wrong_check_call_interface    = 5
        program_start_error           = 6
        program_termination_error     = 7
        x_error                       = 8
        parameter_expected            = 9
        too_many_parameters           = 10
        illegal_command               = 11
        wrong_asynchronous_parameters = 12
        cant_enq_tbtco_entry          = 13
        jobcount_generation_error     = 14
        OTHERS                        = 15.
    p_subrc = sy-subrc.

    IF sy-subrc <> 0.
      CASE sy-subrc.
        WHEN 1.
          MESSAGE e010(0t) WITH 'Sin autorizacion para comando OS ZMV' INTO l_string. "#EC NOTEXT
        WHEN 2.
          MESSAGE e010(0t) WITH 'Comando OS ZMV no existe' INTO l_string. "#EC NOTEXT
        WHEN OTHERS.
          MESSAGE e010(0t) WITH 'ZMV errorcode: ' sy-subrc INTO l_string. "#EC NOTEXT
      ENDCASE.

      WRITE:/ l_string.
    ENDIF.
  ENDFORM.

PUT/GET archivos a/desde servidor FTP

Funciones:

  • FTP_CONNECT
  • FTP_COMMAND
  • FTP_R3_TO_CLIENT
  • FTP_SERVER_TO_R3

Programa ejemplo: RSFTP003

Ejemplo usando clases

CLASS cl_ftp DEFINITION.
  PUBLIC SECTION.
    METHODS: constructor IMPORTING  i_user            TYPE c
                                    i_password        TYPE c
                                    i_host            TYPE c
                                    i_rfc_destination TYPE rfcdest DEFAULT 'SAPFTPA'
                         EXCEPTIONS error,
                 command IMPORTING i_command TYPE c
                         EXPORTING e_subrc   TYPE sy-subrc,
              disconnect EXPORTING e_subrc   TYPE sy-subrc,
                  upload IMPORTING i_xstring  TYPE xstring
                                   i_fullpath TYPE string
                         EXPORTING e_subrc    TYPE sy-subrc.

    DATA g_handle TYPE i.
    DATA g_dest TYPE rfcdes-rfcdest.

ENDCLASS.

CLASS cl_ftp IMPLEMENTATION.
  METHOD constructor.
    CALL FUNCTION 'FTP_CONNECT'
      EXPORTING
        user            = i_user
        password        = i_password
        host            = i_host
        rfc_destination = i_rfc_destination
      IMPORTING
        handle          = g_handle
      EXCEPTIONS
        not_connected   = 1.
    IF sy-subrc <> 0.
      RAISE error.
    ENDIF.
    g_dest = i_rfc_destination.
  ENDMETHOD.

  METHOD command.
    DATA lt_result TYPE TABLE OF text.
    CALL FUNCTION 'FTP_COMMAND'
      EXPORTING
        handle        = g_handle
        command       = i_command
      TABLES
        data          = lt_result
      EXCEPTIONS
        tcpip_error   = 1
        command_error = 2
        data_error    = 3.
    e_subrc = sy-subrc.
  ENDMETHOD.

  METHOD upload.

    DATA: lt_xtable   TYPE faa_t_tenv_xmlraw.
    DATA: ld_filesize TYPE i.
    DATA l_command(120).

* Converting XString to XTable
    CALL METHOD cl_faa_tenv_services=>convert_xstring_to_xtable
      EXPORTING
        id_xstring    = i_xstring
      IMPORTING
        et_xtable     = lt_xtable
      EXCEPTIONS
        invalid_table = 1
        OTHERS        = 2.

    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      e_subrc = sy-subrc.
      RETURN.
    ENDIF.

* . Save XTable
    ld_filesize = xstrlen( i_xstring ).
    DATA l_fname(120).

    l_fname = i_fullpath.
    CALL FUNCTION 'FTP_R3_TO_CLIENT'
      EXPORTING
        fname           = l_fname
        rfc_destination = g_dest
        blob_length     = ld_filesize
      TABLES
        blob            = lt_xtable
      EXCEPTIONS
        command_error   = 1
        data_error      = 2
        OTHERS          = 3.
    e_subrc = sy-subrc.
    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.

  ENDMETHOD.

  METHOD disconnect.

    CALL FUNCTION 'FTP_DISCONNECT'
      EXPORTING
        handle = g_handle.

    CALL FUNCTION 'RFC_CONNECTION_CLOSE'
      EXPORTING
        destination = g_dest
      EXCEPTIONS
        OTHERS      = 1.
    e_subrc = sy-subrc.
  ENDMETHOD.
ENDCLASS.

CLASS cl_documento DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: upload_ftp IMPORTING i_path    TYPE string
                                        i_xstring TYPE xstring
                              CHANGING  c_subrc   TYPE sy-subrc
                                        c_ftp     TYPE REF TO cl_ftp,
      gui_download IMPORTING i_xstring  TYPE xstring
                             i_fullpath TYPE string
                   CHANGING  c_subrc    TYPE sy-subrc.



ENDCLASS.

CLASS cl_documento IMPLEMENTATION.
  METHOD upload_ftp.
    DATA: lt_bin_tab    TYPE solix_tab,
          l_file_length TYPE i.

    DATA: l_user(30) TYPE c VALUE 'mi_usuario',
          l_pwd(30)  TYPE c VALUE '12345678',
          l_host(64) TYPE c VALUE '999.99.9.99',
          l_dest     TYPE rfcdes-rfcdest VALUE 'SAPFTPA'.
    DATA l_command(120).

    DATA: key        TYPE i VALUE 26101957,
          hdl        TYPE i,
          slen       TYPE i,
          x          TYPE i,
          docid      TYPE sysuuid-c,
          cmd(120),
          error,
          bline(120) TYPE x.

    IF c_ftp is NOT BOUND. 
      slen = strlen( l_pwd ).

      CALL FUNCTION 'HTTP_SCRAMBLE'
        EXPORTING
          source      = l_pwd
          sourcelen   = slen
          key         = key
        IMPORTING
          destination = l_pwd.

      CREATE OBJECT c_ftp
        EXPORTING
          i_user            = l_user
          i_password        = l_pwd
          i_host            = l_host
          i_rfc_destination = l_dest
        EXCEPTIONS
          error             = 1.
      IF sy-subrc <> 0.
        c_subrc = sy-subrc.
        RETURN.
      ENDIF.

      c_ftp->command( EXPORTING i_command = 'set passive on'
                     IMPORTING e_subrc = c_subrc
                 ).
      IF c_subrc <> 0.
        RETURN.
      ENDIF.
    ENDIF.

    c_ftp->upload( EXPORTING i_xstring = i_xstring
                             i_fullpath = i_path
                   IMPORTING e_subrc = c_subrc
               ).
    IF c_subrc <> 0.
      RETURN.
    ENDIF.
    l_command = |put { i_path }|.

    c_ftp->command( EXPORTING i_command = l_command
                   IMPORTING e_subrc = c_subrc ).

    IF c_subrc <> 0.
      RETURN.
    ENDIF.

  ENDMETHOD.

  METHOD gui_download.
* Stores a XSTRING as file on frontend client

    DATA: lt_xtable   TYPE faa_t_tenv_xmlraw.
    DATA: ld_filesize TYPE i.

* Converting XString to XTable
    CALL METHOD cl_faa_tenv_services=>convert_xstring_to_xtable
      EXPORTING
        id_xstring    = i_xstring
      IMPORTING
        et_xtable     = lt_xtable
      EXCEPTIONS
        invalid_table = 1
        OTHERS        = 2.

    IF sy-subrc <> 0.
      c_subrc = sy-subrc.
      MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      RETURN.
    ENDIF.

* . Save XTable
    ld_filesize = xstrlen( i_xstring ).
    CALL METHOD cl_gui_frontend_services=>gui_download
      EXPORTING
        bin_filesize = ld_filesize
        filename     = i_fullpath
        filetype     = 'BIN'
      CHANGING
        data_tab     = lt_xtable
      EXCEPTIONS
        OTHERS       = 4.
    c_subrc = sy-subrc.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.


  ENDMETHOD.

ENDCLASS.
DATA g_ftp TYPE REF TO cl_ftp.

...
...
...
loop at .....
  cl_documento=>upload_ftp( EXPORTING i_path = l_path
                                      i_xstring = l_xstring
                             CHANGING c_subrc = l_subrc
                                      c_ftp = g_ftp ).
endloop.

IF g_ftp IS BOUND.
  g_ftp->disconnect( IMPORTING e_subrc = l_subrc ).
  FREE g_ftp.
ENDIF.  


leer archivo binario desde servidor

  • FM C13Z_RAWDATA_READ
FUNCTION C13Z_RAWDATA_READ.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(I_FILE) LIKE  RCGIEDIAL-IEFILE
*"     VALUE(I_LOG_FILENAME) TYPE  FILEINTERN OPTIONAL
*"  EXPORTING
*"     VALUE(E_FILE_SIZE) LIKE  DRAO-ORLN
*"     VALUE(E_LINES) TYPE  I
*"  TABLES
*"      E_RCGREPFILE_TAB STRUCTURE  RCGREPFILE
*"  EXCEPTIONS
*"      NO_PERMISSION
*"      OPEN_FAILED
*"      READ_ERROR
*"      PATH_ERROR
*"----------------------------------------------------------------------

* lokal data -----------------------------------------------------------
* Begin Correction 23.11.2010 1505368 ********************
  DATA: l_log_filename   TYPE fileintern,
        l_stack_tab      TYPE sys_callst,
        l_stack_wa       TYPE sys_calls.
* End Correction 23.11.2010 1505368 **********************

  DATA : L_LEN LIKE SY-TABIX.
  DATA : L_FILENAME LIKE  AUTHB-FILENAME.
* Begin Correction 20.11.2005 899632 *******************
  DATA L_SUBRC LIKE SY-SUBRC.
* End Correction 20.11.2005 899632 *********************


* function body --------------------------------------------------------

* assign value
  l_filename = i_file.

* check the authority for file
  CALL FUNCTION 'AUTHORITY_CHECK_DATASET'
       EXPORTING
*           PROGRAM          =
            ACTIVITY         = SABC_ACT_READ "'READ'
* Authority Check allows right now only 60 Character
            FILENAME         = l_filename(60)
       EXCEPTIONS
            NO_AUTHORITY     = 1
            ACTIVITY_UNKNOWN = 2
            OTHERS           = 3.
  IF SY-SUBRC <> 0.
    RAISE NO_PERMISSION.
  ENDIF.

* Begin Correction 24.09.2010 1505368 ********************


  CASE sy-cprog.
     WHEN 'RC1IMPPG'.
      IF ( I_LOG_FILENAME NE LC_LOG_FILENAME_IMP_PHR_2  AND
           I_LOG_FILENAME NE LC_LOG_FILENAME_IMP_SUB_2  AND
           I_LOG_FILENAME NE LC_LOG_FILENAME_IMP_SRC_2  AND
           I_LOG_FILENAME NE LC_LOG_FILENAME_IMP_PROP_2 AND
           I_LOG_FILENAME NE LC_LOG_FILENAME_IMP_REP_2 AND
           I_LOG_FILENAME NE LC_LOG_FILENAME_IMP_TEMPL_2 ).

* Begin Correction 10.02.2011 1552798  ********************
* code removed
* End Correction 10.02.2011 1552798  ********************
        MESSAGE I153(C$) WITH SY-CPROG SY-REPID SY-SUBRC
                         'C13Z_RAWDATA_READ'.
*       interner System-Fehler! (&1 &2 &3 &4)
        RAISE PATH_ERROR.
      ELSE.
        L_LOG_FILENAME = I_LOG_FILENAME.
      ENDIF.
    WHEN 'SAPLC13E'.
      l_log_filename = LC_LOG_FILENAME_IMP_TEMPL_2.
    WHEN 'SAPLC13G'.
      l_log_filename = LC_LOG_FILENAME_IMP_REP_2.
    WHEN 'RC1TCG3Z' OR
         'RC1TCG3Y'.
      l_log_filename = LC_LOGICAL_FILENAME_FTAPPL_2.
    WHEN 'SAPLC14S'.
* Begin Correction 10.02.2011 1552798  ********************
      l_log_filename = LC_LOG_FILENAME_ARCHIVLINK_2.
* End Correction 10.02.2011 1552798  ********************

    WHEN OTHERS.
     MESSAGE I153(C$) WITH SY-CPROG SY-REPID SY-SUBRC
                          'C13Z_RAWDATA_READ'.
*     interner System-Fehler! (&1 &2 &3 &4)
     EXIT.
  ENDCASE.

* validate physical filename against logical filename
* Begin Correction 10.02.2011 1552798  ********************
  CALL FUNCTION 'FILE_VALIDATE_NAME'
    EXPORTING
      logical_filename  = L_LOG_FILENAME
    CHANGING
      physical_filename = I_FILE
    EXCEPTIONS
     OTHERS          = 1.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    RAISE PATH_ERROR.
  ENDIF.
* End Correction 10.02.2011 1552798  ********************
* End Correction 24.09.2010 1505368 ********************

* read the raw-file from the appl.server
* Begin Correction 20.11.2005 899632 *******************
  CLEAR L_SUBRC.
  CATCH SYSTEM-EXCEPTIONS OPEN_DATASET_NO_AUTHORITY = 1
                          DATASET_TOO_MANY_FILES = 2
                          OTHERS = 4.
    OPEN DATASET I_FILE FOR INPUT IN BINARY MODE.
    L_SUBRC = SY-SUBRC.
  ENDCATCH.
  IF SY-SUBRC <> 0 OR
      L_SUBRC <> 0.
    RAISE OPEN_FAILED.
  ENDIF.
* End Correction 20.11.2005 899632 *********************

* Begin Correction 21.03.2005 816266 *******************
  CATCH SYSTEM-EXCEPTIONS DATASET_READ_ERROR = 11
                          OTHERS = 12.
    DO.
    CLEAR L_LEN.
    CLEAR E_RCGREPFILE_TAB.
    READ DATASET I_FILE INTO E_RCGREPFILE_TAB-ORBLK LENGTH L_LEN.
    IF SY-SUBRC <> 0.
* Begin Correction 20.11.2005 899632 *******************
      IF L_LEN > 0.
* End Correction 20.11.2005 899632 *********************
        E_FILE_SIZE = E_FILE_SIZE + L_LEN.
        APPEND E_RCGREPFILE_TAB.
      ENDIF.
      EXIT.
    ENDIF.
    E_FILE_SIZE = E_FILE_SIZE + L_LEN.
    APPEND E_RCGREPFILE_TAB.
  ENDDO.
  ENDCATCH.
  IF SY-SUBRC > 10.
    RAISE READ_ERROR.
  ENDIF.
* End Correction 31.03.2005 816266 *********************

  DESCRIBE TABLE E_RCGREPFILE_TAB LINES E_LINES.

* Begin Correction 20.11.2005 899632 *******************
  CATCH SYSTEM-EXCEPTIONS DATASET_CANT_CLOSE = 1
                          OTHERS = 4.
    CLOSE DATASET I_FILE.
  ENDCATCH.
  IF NOT SY-SUBRC IS INITIAL.
*  but there wasn't any error at the reading of the data
  ENDIF.
* End Correction 20.11.2005 899632 *********************

ENDFUNCTION.

Unix Varios

Trn. SM69 : Comandos Sistema Operativo

Trn. CG3Z / Reporte RC1TCG3Z : Upload fichero a servidor

Trn. CG3Y / Reporte RC1TCG3Y : Download fichero desde servidor

Trn. CACS_FILE_COPY, Rep. CACS_FILE_COPY : copiar de servidor a PC y viceversa

Rep. RSBDCOS0 : Ejecutar comando SO (grabado en log sistema y ficheros Trace)

FM. BRAN_DIR_CREATE : crear directorio (ojo con las validaciones internas pero se puede usar de base)