Sunday, May 6, 2018

Executing External Commands in an ABAP

Maintaining External Commands

OS Host External commands are maintained using Transaction SM69. As you can see from the screen shot below, a lot of commands are delivered by SAP in every NetWeaver system; consequently, it’s always a good idea to see if SAP has already configured the command you’re looking to execute instead of creating a new command definition from scratch.
SM69 Maintain External Commands
OK, let’s take a deeper look. In order to show you how to configure your own custom external commands, let’s look at one delivered by SAP. The PING command is available on any SAP NetWeaver AS ABAP host. The PING command will determine whether an IP address is reachable on a network.

Put your cursor on the PING command as shown above and double-click or hit the display icon.
Ping Command
As you can see in screen shot above, the definition of an external command is broken up into two distinct parts.

Fields in the COMMAND Group Box

In the Command group box, you must configure the following:
- In the Command name field, you must assign a unique name to the command. This name must be defined in the proper namespace (i.e., prefixed with a Y or Z for the customer namespace, etc.).
- The Operating System field defines the operating system or systems that support this command. In the case of the PING command, the generic ANYOS value was assigned to indicate that the PING command can be run on any host operating system. However, because some external commands are OS-specific, this field is necessary.
- The Type field refers to whether or not the command was defined by SAP or a customer.

Fields in the DEFINITION Group Box

The actual definition of the command occurs within the Definition group box. Here you define the following:
- In the Operating System Command field, define the command to be executed. This can be a built-in OS command, a path to an executable on the host system, and so on.  In our case “PING”
- In the Parameters for Operating System Command field, you can define parameters to be passed to the OS command at runtime. These parameters are static parameters that will always be passed to the command.
- If you want to allow dynamic parameters to be passed into the command, you need to select the Additional Parameters Allowed check box. These parameters are passed to the command at runtime using the API functions.
- The Trace checkbox determines whether or not you want the framework to turn on a trace when the command is being executed.
- In the Check Module input field, you can plug in a function module that is called by the framework to validate the external command before executing it. This function module must match the signature of the SXPG_DUMMY_COMMAND_CHECK function module provided with the system. This will run before the command is executed and if an exception is raised by the check module, the external command isn’t executed. This works like an EVENT or Workflow Check Function.

Testing The External Command

OK, so we want to test an SAP delivered command, or we just created a “Z” Command and we would like to test it. To test an external command, select the command and click on the Execute button in the toolbar Code Inspector

Ping Test
Here, you have the option of plugging in test parameters in the Additional Parameters field. You can also select an execution target to test the command on other application server hosts. I will use Yahoo’s site, and attempt to PING it. When you click on the Execute button, the external command is executed, and you’re navigated to the results screen shown below. Here, you can see the return code of the command, as well as the generated output.
Ping Test Results

Executing External Commands in an ABAP

To execute external commands within an ABAP program, you can use the standard function module SXPG_COMMAND_EXECUTE. To demonstrate how this works, let’s code a quick little DEMO program. This program accepts a host name as a parameter and uses it as an argument to the PING command. The PING command is executed via a call to the standard API function. The code is below.
REPORT zdemo_ping.
 CLASS lcl_command_interface DEFINITION.
   PUBLIC SECTION.
     CLASS-METHODS:
       execute_command IMPORTING im_command_name
                                   TYPE sxpgcolist-name
                                 im_param_string
                                   TYPE btcxpgpar.
 ENDCLASS.
 CLASS lcl_command_interface IMPLEMENTATION.
   METHOD execute_command.
     "Method-Local Data Declarations:
     DATAlv_status  TYPE extcmdexex-status,
           lv_retcode TYPE extcmdexex-exitcode,
           lt_output  TYPE STANDARD TABLE OF btcxpm.
     FIELD-SYMBOLS:
     <lfs_output> LIKE LINE OF lt_output.
     "Execute the selected command:
     CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
       EXPORTING
         commandname                   im_command_name
         additional_parameters         im_param_string
         operatingsystem               'ANYOS'
       IMPORTING
         status                        lv_status
         exitcode                      lv_retcode
       TABLES
         exec_protocol                 lt_output
       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.
     "Display the results of the command:
     LOOP AT lt_output ASSIGNING <lfs_output>.
       WRITE/ <lfs_output>-message.
     ENDLOOP.
   ENDMETHOD" METHOD execute_command
 ENDCLASS.
 PARAMETERS:
 p_host TYPE btcxpgpar LOWER CASE OBLIGATORY.
 
 START-OF-SELECTION.
   "Execute the 'PING' command:
   lcl_command_interface=>execute_command(
   im_command_name 'PING'
   im_param_string p_host ).
Let’s execute the program… You will the Selection Screen below asking for a Host Name.
Zdemo Ping Program

No comments:

Post a Comment