Вы находитесь на странице: 1из 6

Print

Performing FTP Commands From ABAP


Thomas Jung
Business Card
Company: Kimball International
Nov. 15, 2004 12:14 PM
Permalink

RSFTP005, to generate the RFCdestinations for us.


RSFTP002 test program

Introduction
The use of FTP from ABAP seems to have been a hot topic on the ABAP Forums of late. I thought I might sit
down and document whatever I could find on this subject and share it as a weblog.
Over the years I have seen lots of different solutions for moving files on and off of a SAP system. I have seen
external FTP scripts written in OS shell languages. I have seen full blown custom applications that are made to
interface to the SAP system. However I think you will find that most of the technology you need to perform a
simple FTP from ABAP is already contained in the standard system. All of my examples and screen shots will be
coming from a 46C system.
SAP's Solution
If you have ever taken a look at the kernel directory of your SAP system, you might have noticed an interesting
little executable: sapftp.exe (the name of the file on Windows SAP Kernels). It is this part of the Kernel that
exposes FTP functionality to the ABAP Programming language.
So you have a suspicion that there is FTP functionality in ABAP, but you’re not quite sure how to use it. Where
do you start? I always turn to the Service Marketplace first. A quick search on SAPFTP reveals there is an entire
component (BC-SRV-COM-FTP) on the subject. The most general note and our starting place is OSS Note 93042.
This note starts off with a nice description of what SAPFTP is: A client RFC application that is accessed via RFC
from ABAP. But we also find out that in addition to SAPFTP being part of the kernel, it is also part of the SAPGui.
That means that we can perform FTP commands originating from our R/3 Server or from a Client Workstation.
Well if this solution is accessed via RFC, then we must have to setup some RFC destinations. In fact we have two
that we need; SAPFTP for Front-end FTP and SAPFTPA for access on the application server. Luckily we
don't even have to mess with setting these RFC destinations up in SM59. SAP has supplied a program,
RSFTP005, to generate the destinations for us.
Now before we go off and start written code on our own to hit these FTP functions, why don't we make sure
everything is setup and working. Once again SAP has helped us out by providing us with a test program,
RSFTP002. (In case you are wondering the FTP functionality and many other test programs are all contained in
SAP Development Class SFTP). When we run this test, we get a set input parameters for the server, username
password, etc. We want to start out simple and just make sure we are getting a connection. Therefore we will
just execute the pwd command (Print Working Directory).

Your answer back should look something like this:


If you are wanting to see a list of FTP commands, try using the command HELP in place of PWD:

If something did go wrong during the test, I suggest that you active the trace option in SM59 for the FTP
Destination. You can then use program RSFTP001 to display the current trace file.
Programming the FTP
Not only does the RSFTP002 program give us a test environment, but it also provides us with a programming
example. We can see that the FTP functionality is really provided by a set of function modules all within the
SFTP Function Group. We have the basic commands such as FTP_CONNECT, FTP_COMMAND, and
FTP_DISCONNECT that can be strung together to create a complete file operation action. The FTP_COMMAND
Function allows you to issue arbitrary FTP commands as long as the SAPFTP function, the Host, and the
Destination server all support the command. Then you have the specialized functions such as
FTP_R3_TO_SERVER, FTP_R3_TO_CLIENT, and FTP_CLIENT_TO_R3. This lets you take some data in memory
and transfer it someplace else. This has the advantage of not having to write the data to the file system first and
not to have to issue any FTP commands. However these functions are also limited to the scope described.
If you are already familiar with FTP in general, working with these function modules should not seem to difficult.
The Connect, Command, Disconnect actions would seem somewhat self explanatory. So instead of looking at the
entire program in detail let's focus on two things that may be unfamiliar. First the program starts off with an
ABAP Kernel System call to AB_RFC_X_SCRAMBLE_STRING. Well we don't want to pass a potentially sensitive
password openly. Therefore the FTP_CONNECT function module requires that the password be encrypted before
it receives it. It is this System call that performs that one-way encryption. Now I checked a 620 SP42 system and
in this example, SAP has replace the AB_RFC_X_SCRAMBLE_STRING with a function call to HTTP_SCRAMBLE.
Unfortunately HTTP_SCRAMBLE doesn't even exist in my 46C system. The only other thing that I wanted to
point out about these function calls is the exporting parameter on the FTP_CONNECT. It passes back a
parameter called handle. This handle then becomes an importing parameter to all subsequent calls:
FTP_COMMAND and FTP_CLOSE. This handle is the pointer to the instance of FTP that we started with the
FTP_CONNECT. This assures that we get reconnected to the same FTP session with each command we issue.
FTP Development
I thought I would share a few of the things that can be built using this FTP functionality. First off I didn't want a
bunch of ABAP programs directly working with the SAP FTP Function modules. As you can see there is already a
difference in the examples for encrypting the password between 46C and 620. Therefore I thought it would be
best to encapsulate all the FTP function in one custom ABAP OO Class. Not only did I get the opportunity to hid
the inner SAP functionality and make it easy to switch out during upgrades, but I also get consistent error
handling as well. I accept the User Name, Password, Host, and RFC Destination in during the Constructor of the
class. I then store these values away in Protected Attributes. Each function module is then implemented as a
Instance Method. The Password encryption functionality is then all tucked away nicely in the class. Also the
calling program doesn't have to worry about keeping track of the FTP handle either since it is an instance
attribute as well.

Next I got really carried away. I wanted a way to record entire FTP scripts that could be filled with values at
runtime and ran as a step in a background job. My company used to have many interfaces that ran frequently
sending files all over the place. We needed a mechanism to monitor and support these file moves. This was
really the root of this tool, but it also gives you an idea of how powerful these functions can be.

Closing
I hope that anyone interested in FTP from ABAP will find this resource useful. If anyone has any other resources
that should be included here, feel free to post them.
Thomas Jung is an Application Developer for Kimball Electronics Group (www.kegroup.com) and a huge fan of
ABAP.

Comment on this weblog

Showing messages 1 through 7 of 7.

Titles Only Main Topics Oldest First


← New to SAP/ABAP-4
2005-02-24 13:03:06 Thomas Godfrey Business Card [Reply]

I've been in IT for over 30 years as a mainframe contract programmer/analyst. Recently a company hired me for
my legacy knowledge of there business to become a developer on SAP for these same systems.

My first task is to develop a generalized ftp application to send data to the mainframe and then on to financial
institutions. I have created an ABAP/4 test program with literals for the dest/host etc. And it works fine. I need
more detail/guidance in creating the Class/Method/Function you have described to make this parameter driven
and callable by other programs. I know this is a lot to ask, any help in pointing me in the right direction would
be greatly appreciated. When I mention class creation to the existing staff they all say "Oh, we don't use or
know anything about that object oriented stuff, we just right programs."

Thanks,
Tom
← New to SAP/ABAP-4
2005-02-24 13:13:33 Thomas Jung Business Card [Reply]

"we don't use or know anything about that object oriented stuff" That's something I might have expected to
hear 4 years ago. That's kind of like saying that you only want to use half of the programming lanuage (the old
half at that).

However it is true that you have to learn to walk before you run. There are some fundamentals to ABAP that are
usefull to have down before you start in with ABAP OO. But I wouldn't let that hold you back for long.

I'm afraid that I wouldn't be able to teach you everything you would need to know to create a function module
or a class in this posting. There was a nice weblog that was just posted the other day that had the steps to
create your first simple class. I suggest you start there.

As far as my FTP class goes, I don't mind sending you the code for the class. My email address is already all
over SDN (and spammers' lists) so just send me the address that you want the details sent to. My email is
tjung@kimball.com.
← New to SAP/ABAP-4
2005-02-24 13:05:42 Thomas Godfrey Business Card [Reply]

I just read what I typed, I meant "write programs" not "right programs" ... duh

← Good suggestions - now...


2004-11-24 09:15:27 Jacques Claassen Business Card [Reply]

...any chance you've used FTP via an HTTP proxy?

Any help using the HTTP* functions in the ZFTP function group will be greatly appreciated. Any one?

Thx
← Good suggestions - now...
2004-11-24 09:59:11 Thomas Jung Business Card [Reply]

I'm afraid I haven't done either. However I have used the HTTP client functionality in the WebAS 620
(if_http_client) along with an HTTP proxy. I have an example of this in another weblog.
← Pretty insightful
2004-11-15 19:27:26 Subramanian Venkateswaran Business Card [Reply]

Thanks for this weblog, and I can be pretty sure there are some(many) more weblogs in the waiting.

Regards,
Subramanian V.

← I have been using a local include ...


2004-11-15 16:29:39 Swapan Sarkar Business Card [Reply]

On the other hand I have been using a local include of ZCL_FTP_BASE in my programs for some time. Here's the
signature:

<quote>
*&-----------------------------------------------*
*& Include ZHR9_FTP_CLASSES
*&-----------------------------------------------*
*& Utility class ZCL_FTP_BASE having all the
* functions to upload and download files from an
* FTP server.
*&-----------------------------------------------*

CLASS zcl_ftp_base DEFINITION.


PUBLIC SECTION.
* Output file type
TYPES: BEGIN OF t_scratch,
data(1024) TYPE c,
END OF t_scratch,
t_str_tab TYPE STANDARD TABLE OF t_scratch.

CONSTANTS: co_crlf(2) TYPE x VALUE '0D0A'.

DATA: status TYPE c VALUE space.

METHODS: constructor IMPORTING im_user TYPE string im_pwd TYPE string


im_server TYPE string,
send_table_data IMPORTING im_file TYPE string
im_table TYPE t_str_tab,
get_table_data IMPORTING im_file TYPE string
EXPORTING ex_table TYPE t_str_tab,
send_string_data IMPORTING im_file TYPE string
im_string TYPE string,
get_string_data IMPORTING im_file TYPE string
EXPORTING ex_string TYPE string,
disconnect.

PRIVATE SECTION.
DATA: v_user(64) TYPE c,
v_pwd_clear(30) TYPE c,
v_pwd(64) TYPE c,
v_server(30) TYPE c,
v_filename TYPE rlgrap-filename,
v_handle TYPE i.
METHODS: pwd_scramble IMPORTING im_pwd TYPE c.

ENDCLASS. "ZCL_FTP_BASE DEFINITION


</quote>