SFTP from SAP using Winscp Client

We use FTP to transfer sales order information to Hauliers; this is important piece of information for them to plan their resources (Drivers, Truck etc.). We are using FTP on site-to-site VPN to make communication secure. For infrastructure reasons we are moving away from site-to-site VPN and requirement is to use SFTP.

SAP ECC does not support sftp this is how I managed to achieve sftp communication using open source Winscp sftp client. Note that we have SAP server installed on Windows 2003 server and you will have to adapt some steps for other server OS.

Step 1: Download and install Winscp client.

Download Winscp portable executables from http://winscp.net/eng/download.php . It’s a zip file, with an exe and com file. Unzip the content and put it on server from where your SAP can execute them. I have kept mine in externexe/sftp folder. One first execution it will automatically create ini file so don’t worry if you haven’t got ini file.


Step 2: Check installation and connectivity

Before we start with SAP side of development let’s check if this client is working.

Usually Winscp client opens up a session with server where you execute commands and close it when done. But here we want to open session, execute commands and close session all in one go and fortunately Winscp does support this.

A simple dir command along with open session and exit command look like this

winscp.com /command "option batch abort" "open ftp://username:password@host_ip" "dir" "exit"

sftp sap

Option batch abort prevent session to seek for user input if anything goes wrong. For example, if your user id don’t have authorisation on sftp server then Winscp client will prompt for Retry which will not be desirable if we are executing command from SAP. You can execute multiple commands here just add them to list before exit in double quotes.

A file transfer command from a specific directory on client (F:/data) to specific directory on sftp server (in) will look like this.

winscp.com /command "option batch abort" "open ftp://username:password@host_ip" 
"lcd F:\Data" "cd in" "put data.txt" "exit"

Step 3: Create command in SM69 which will call Winscp.com

Make sure to check Additional parameters allowed.

Press save.

To test the command from SAP press execute and input “open “sftp://username:password@host_ip”” “dir” “exit” in additional parameters and execute.

You should get same output which you got in command prompt. Notice variable return code. Zero value signifies successful execution.


Step 4: Calling external command ZFTP using function module SXPG_COMMAND_EXECUTE.

This is sample code will give you an idea how to call external command we just created. You will have to make some changes to make it work at your place.

METHOD execute_all_commands.

  DATA : lv_additional_parameters TYPE string ,
         lv_additional_param_func TYPE sxpgcolist-parameters .

* Create additional parameters strings based on username, password and host ip
  IF me->username  IS INITIAL .
    RAISE EXCEPTION TYPE zcx_sfp
      EXPORTING
        textid = zcx_sfp=>user_name_empty .
  ENDIF.

  IF me->password IS INITIAL .
    RAISE EXCEPTION TYPE zcx_sfp
      EXPORTING
        textid = zcx_sfp=>password_empty .
  ENDIF.

  IF me->host_ip IS INITIAL .
    RAISE EXCEPTION TYPE zcx_sfp
      EXPORTING
        textid = zcx_sfp=>password_empty .
  ENDIF.

* winscp.com /command "open "sftp://username:password@host_ip"" "dir" "exit"

* Create open command
  CONCATENATE '"open'
              ' "sftp://'
              me->username
              ':'
              me->password
              '@'
              me->host_ip
              '""'
         INTO lv_additional_parameters .

* Add user commands
  CONCATENATE lv_additional_parameters
              me->additional_commmands
         INTO lv_additional_parameters
        SEPARATED BY space .

* Add exit command in the end
  CONCATENATE lv_additional_parameters
              '"exit"'
         INTO lv_additional_parameters
        SEPARATED BY space .

  IF STRLEN( lv_additional_parameters ) > 255  .
    RAISE EXCEPTION TYPE zcx_sfp
      EXPORTING
        textid = zcx_sfp=>command_too_long .
  ENDIF.


  DATA : lv_status   TYPE extcmdexex-status   ,
         lv_exitcode TYPE extcmdexex-exitcode ,
         li_return   TYPE lca_tracefile_tab   .

  lv_additional_param_func = lv_additional_parameters .

  CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
    EXPORTING
      commandname                   = me->command_name
      additional_parameters         = lv_additional_param_func
    IMPORTING
      status                        = lv_status
      exitcode                      = lv_exitcode
    TABLES
      exec_protocol                 = li_return
    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 OR lv_exitcode <> 0.
    RAISE EXCEPTION TYPE zcx_sfp
      EXPORTING
        textid = zcx_sfp=>error_sftp
        exit_code = lv_exitcode
        status = lv_status
        sftp_log = li_return   .
  ENDIF.

  return = li_return .

ENDMETHOD.

One Reply to “SFTP from SAP using Winscp Client

  1. Hi, very useful info..

    I have a question like, can we use this scenario between two WINDOWS systems? In my scenario it is like SAP(sender) is deployed on Windows and other(receiver) is also Windows System. Can we execute the WINSCP commands in windows system also?

Leave a Reply