You are on page 1of 3

Often you need to execute command of operating system from ABAP Program, the

purpose may be for encrypt or decrypt file and compres or decompress file, and also you
need supply user input to OS Commnands.

In ABAP you can use two method, the difference from both ways is just on the security
system. but if you are consent you can choose the method that SAP is recommended.

1.Using SXPT Function Group to execute OS


Command from ABAP Program
First you need to define command in SM69, the function module will check user
authorization before execute the command, there are three function modules available to
execute OS Command.

 SXPG_CALL_SYSTEM
this function module will execute os command on the locally.

 SXPG_COMMAND_EXECUTE
this function module will execute os command on the target host

 SXPG_COMMAND_EXECUTE_LONG 
this function module will execute os command on the target host but support longer list
of parameter.

This method is SAP recommended, because it function module also consent about
security.

1 REPORT Z_TEST_PING.
2  
3 TYPES char255 TYPE c LENGTH 255.
4 PARAMETERS: pv_count TYPE i DEFAULT 5,
5             pv_targt TYPE char255 LOWER CASE.
6 DATA lt_result TYPE TABLE OF btcxpm WITH EMPTY KEY.DATA lv_line TYPE btcxpm.DATA lv_param TYPE
7 char255.
8 INITIALIZATION.
9   CALL 'C_SAPGPARAM' ID 'NAME'  FIELD 'SAPDBHOST'
10                      ID 'VALUE' FIELD  lv_param.
11   pv_targt = lv_param. ” get the db server as default target
12 AT SELECTION-SCREEN.
13   if pv_count < 1 OR
14      pv_count > 10.
15     MESSAGE ‘Please enter a number between 1 and 10.’ TYPE ‘E’.
16   ENDIF.
  if pv_targt = ”.
17     MESSAGE 'pv_target: You need to specify a destination to ping.' TYPE 'E'.
18   ENDIF.
19 START-OF-SELECTION.
20 lv_param = |–c{ pv_count } { pv_targt }|. ” this only works on UNIX like ping this wayWrite :/ ‘now executing:
21 Z_MyPing with parameters: ‘ && lv_param.Write :/ .CALL FUNCTION ‘SXPG_CALL_SYSTEM’
22   EXPORTING
23     commandname           = 'ZPING'
24     additional_parameters = lv_param
25   TABLES
26     exec_protocol         = lt_result
27   EXCEPTIONS
28     no_permission         = 1
29     command_not_found     = 2
30     security_risk         = 3
31     OTHERS                = 4.
32   IF SY–SUBRC = 0.
33       LOOP at lt_result into lv_line.
34         Write :/ lv_line–message.
35       ENDLOOP.
36   ELSE.
37       MESSAGE 'Error, return code ' && sy–subrc TYPE 'E'.
  ENDIF.
 

2.Using CALL System function call to execute OS


Command.
You can execute OS command using CALL system function call to execute OS
Command, the cons using this method it is not consent about security and you can run
most of OS command without any restriction.

Most of SAP Basis always deactivate this function by setting system profile
rdisp/call_system to ‘0’.

For example we want to encrypt string using openssl Linux OS Program from ABAP
Program and get the result.

1   DATA: lv_cmd     TYPE char1024,


2         lt_result2 TYPE TABLE OF char1024 WITH HEADER LINE.
3  
4 REFRESH: lt_result2.
5   CLEAR: lv_signature_enc.
6   CONCATENATE 'echo -n ''' lv_signature '''' INTO lv_cmd.
7  
8   CONCATENATE 'pass:' lv_passp INTO lv_passin.
9   lv_keypath = sy-sysid. TRANSLATE lv_keypath TO LOWER CASE.
10   lv_keypath = '/home/jsdadm/JSD_ssl.key'.
11   CONCATENATE  lv_cmd '| openssl sha1 -sign' lv_keypath '-passin' lv_passin '| base64'
12     INTO lv_cmd SEPARATED BY space.
13   CALL 'SYSTEM' ID 'COMMAND' FIELD lv_cmd
14                 ID 'TAB'     FIELD lt_result2[].
15   IF sy-subrc EQ 0.
16     LOOP AT lt_result2.
17       CONCATENATE lv_signature_enc lt_result2 INTO lv_signature_enc.
18     ENDLOOP.
19   ENDIF.

You might also like