F How to execute an external OS commands in SAP ABAP? | CodeTheta

How to execute an external OS commands in SAP ABAP?

April 01, 2024

Code: 

DATA: obj TYPE REF TO zcl_call_script.
DATA : lv_command      TYPE sxpglogcmd,
         lv_targetsystem TYPE rfchost_ext.

    CREATE OBJECT obj.
    lv_command = 'ZCOMMAND'.

    IF sy-sysid EQ 'GWD'.
      lv_targetsystem = 'sapr3d'.

      CALL METHOD obj->zcall_shell_script
        EXPORTING
          im_commandname  = lv_command
          im_targetsystem = lv_targetsystem.

    ELSEIF sy-sysid EQ 'GWP'.
      lv_targetsystem = 'sapr3p'.

      CALL METHOD obj->zcall_shell_script
        EXPORTING
          im_commandname  = lv_command
          im_targetsystem = lv_targetsystem.

    ENDIF.

--------------------------------------------------------
Class Name  - zcl_call_script
Method Name - zcall_shell_script

Code:
METHOD zcall_shell_script.

    DATA: lv_status TYPE btcxpgstat,
          lv_txt    TYPE string.

    CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
      EXPORTING
        commandname                   = im_commandname
        operatingsystem               = sy-opsys
        targetsystem                  = im_targetsystem
      IMPORTING
        status                        = lv_status
      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.
    IF sy-subrc <> 0.
      CONCATENATE sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO lv_txt.
      WRITE: lv_txt.
    else.
      write: 'success'.
    ENDIF.

  ENDMETHOD.

IDE Used To Test This Code : SAP ABAP Editor.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us. We will reply you as soon as possible.

Post a Comment