Difference between revisions of "SAP ABAP UNIX"

From SapWiki
Line 203: Line 203:
 
           l_file_length TYPE i.
 
           l_file_length TYPE i.
  
     DATA: l_user(30) TYPE c VALUE 'svc_stockmed',
+
     DATA: l_user(30) TYPE c VALUE 'mi_usuario',
           l_pwd(30)  TYPE c VALUE '5Vc_5t0CKm3D',
+
           l_pwd(30)  TYPE c VALUE '12345678',
           l_host(64) TYPE c VALUE '172.22.0.21',
+
           l_host(64) TYPE c VALUE '999.99.9.99',
 
           l_dest    TYPE rfcdes-rfcdest VALUE 'SAPFTPA'.
 
           l_dest    TYPE rfcdes-rfcdest VALUE 'SAPFTPA'.
 
     DATA l_command(120).
 
     DATA l_command(120).

Revision as of 14:11, 4 March 2021

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.  


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)