.
Showing posts with label List of FTP Commands. Show all posts
Showing posts with label List of FTP Commands. Show all posts

FTP commands

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).
image

Your answer back should look something like this:
image

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

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.
image

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.
image

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.

List of FTP Commands

File Transfer Protocol is a wonderful tool. It is essentially a networking protocol, that allows one to easily obtain the files he or she desires, such as media (for example, audio or video files), data (for example, .pdf files that span hundreds of megabytes), or even software. It has a lot of practical uses, which include the fact that it breaks the limitations of email (and all the other file-sharing applications one might use), which does not allow users to send or receive a file bigger than a certain size, at one time, and also, it allows only authorized persons to access your stored files, that too from a remote location. Another way to benefit from using FTP is to yourself access your personal files from a remote location by logging into your own computer with the help of it.

Of course, using FTP, just like any other internet-based service, comes with concerns of reliability, as it is evident that sending large amounts of data over long physical distances over a network would open up opportunities of hacking. However, these issues can be worked around by implementing a few network security measures, such as encrytion of data, or using FTP along with other secure protocols. At the end of the day, the truth is, the beauty and simplicity of transferring your larger files entirely into another computer, that too located elsewhere, without having to use external memory devices, nor to trouble yourself with compressing the file so as to make it compatible with email, just by using FTP, is unsurpassed.

If you are now sold on the idea of using FTP to fulfill all your file-sharing needs, you would also be concerned about whether it is terribly difficult to figure out the nuances of FTP; whether you need to learn it, like a programming language. The answer is, no, it is incredibly simple, all you need to do is to familiarize yourself with the commands so that you can use them as and when they are required. It is even more simple to pick up if you are already familiar with programming on UNIX. Buzzle provides you a complete list of all FTP commands, and what they are used for, so that irrespective of whether your system runs Windows, Linux, UNIX, or any other OS, you can easily make use of this handy method for transferring your files. For beginners, we have also formulated a guide to log in to the remote server. Click on this link if you are just looking for a comprehensive list of FTP commands.


Getting Started: Logging into the Remote Computer

☛ Before beginning an FTP session, one needs to obtain basic information about the server host (to be read, from now on, as the remote computer of which you wish to access files) such as its domain name (if available) or at least its network address.

☛ Your own computer needs to be equipped with a suitable FTP client. This is important, especially if the computer you wish to connect to is running a different operating system. You will be typing in commands in the FTP client. Different FTP client software are available for download (mostly they are free and open-source, but some are also chargeable), for example Filezilla, and generally, each has a UI-integrated login page, where one needs to enter server information.

☛ One can also connect to the FTP server by starting a session over one's browser. For this, you need to know the FTP address of the server. An FTP address has the following syntax:

ftp://username:password@ftp.serverhostname.com:21
where,
username and password → It may be optional to enter a username, and/or a password depending on the configuration of the FTP server. If it is not needed, the part of the address up till the '@' can be eliminated in the FTP address.
serverhostname → Either the server's hostname or its IP (network) address is mandatory.
21 → This value is the default TCP port number assigned to the FTP protocol. If you are using a different port number (in the event that you are using a different flavor of FTP), then you need to replace this value with the respective port number. In any case, mentioning the port number is preferred so that your computer explicitly knows what port number to use, although mostly, people choose to eliminate this parameter.

☛ In the event that you do not have an FTP client software installed, the terminal (or the command prompt) can be used as your FTP client. Open the respective utility in your operating system.

✐ In Windows, one can open the MS DOS command prompt by opening the Start Menu, and typing "cmd" in the Search Box, followed by the Enter ↵ key.
✐ If you are using a Mac computer, you can find the Terminal inside the Utilities folder in the Applications menu.
✐ In Linux-based OS computers, one can open the Terminal by pressing the Ctrl, Alt, and T keys.

Now, in the terminal (or prompt) type the following

C:\>ftp serverhostname.com
Where serverhostname.com refers to either the domain name of your server, or its IP address.

After this step, your client will prompt you for the username and/or password, if the server requires it. One can log in to the FTP server in the anonymous mode too, wherein although files can be downloaded from it, one can neither make changes in the directory structure, nor modify any files. If your server supports anonymous login, it will specify this before asking you for a username and password. If you wish to log in as an anonymous client, you need to enter your username as "anonymous", and simply press the Enter ↵ key when you are asked for the password.

C:\>ftp serverhostname.com
Connected to serverhostname.com.
220 You are allowed to login as an anonymous user.
User >: username
331 Password required for serverhostname.com.
Password:
230 User serverhostname.com logged in.
ftp:\>_
Where, username needs to be replaced by your actual username, or "anonymous".

☛ Now that you have successfully logged into the desired FTP server, you can now use any of the FTP commands to browse or navigate through and return from the directories (or folders) available on the server, as well as download and upload files.