Integrating Secure FTP into Data Services SAP Data Services includes decently-robust native support for FTP transport, as long as you don t mind it being non-secured. However, understandably, many applications require the use of secured FTP transport methodologies, since the data being transmitted is often of a sensitive nature. You probably don t want to risk your data being intercepted in transit, especially if it s going outside of the organization s firewall. Fortunately, programs like WinSCP are available, and are robust, scriptable, and free. We can then integrate that scripting into Data Services to create a relatively seamless solution. What you ll Need WinSCP (available at winscp.net) Data Services A text editor FTP credentials and protocol information Known directory structures on both source and target systems A little bit of time In this example, we ll copy over a remote directory s contents to a local directory. Step 1: Install WinSCP Download WinSCP at winscp.net. You can run a pretty standard install. Make note of the installation directory Set the program to use.ini configuration, instead of the default, which is to write configuration to the registry. I ll explain why (and how to fix it if you ve already goofed) below. Step 2: Make Sure you re Using.ini Configuration WinSCP stores remote server keys in its configuration area. Generally, the first time you connect to a remote server, you need to accept the remote key. After this, it s stored in that area for any future uses. Since we want all of this scripted, we want to make sure there is no user input required. If there is, the entire process will fail, since Data Services will interpret the prompt as an execution failure. Odds are that you re probably using the local system account to run Data Services. Therefore, when we launch WinSCP from Data Services, it will also launch under the system account. If configuration is stored in the registry, it s stored in HKEY_LOCAL_USER, which means the configuration (and, therefore, the stored key) is only available to the user that s logged in when the key is accepted. That s a problem, since we can t create registry settings for the system account.
However, the.ini file is stored in the same directory as WinSCP program files, and is available to any user running the program globally. So, when you accept the key manually the first time you run the script, the key is saved for all future executions no matter which account the script uses to run. To make sure this is set, start WinSCP. Navigate down to Preferences at the bottom of the left pane. Then, click the Preferences button toward the middle of the window. In the preferences window, select
Step 3: Write your WinSCP Script As discussed earlier, I m including a basic script that copies down the contents of a remote directory of text files to a local directory. If you need additional functionality, the WinSCP site has excellent help modules in available commands. If you need to get fancy, you can even write scripts in.net. Here, we ll be sticking to the simple script format. # Automatically abort script on errors option batch off # Disable overwrite confirmations that conflict with the previous option confirm off # Connect using supplied credentials. Hostkey should work as-is 99% of time open sftp://user:password@server -hostkey="ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" # Change remote directory cd /remotedir # Force ascii mode transfer for text files option transfer ascii # Download files. Make sure the local directory ends with a backslash get *.* c:\localdir\ # Disconnect close # Exit WinSCP exit Step 4: Test the WinSCP Script Run your script using the following syntax in a command line: [WinSCP location]\winscp.com /script=[script location] If you connect successfully, it s likely that you ll be prompted to accept the certificate on your first successful run. Please do so (select Y ). This will make sure the prompt doesn t appear again. If the entire script is successful, you should now see the contents of the remote directory copied into your local directory. If not, keep on trying. When you re successful, please keep a copy of the command line you used. Make sure both the WinSCP location and the script location both have full paths included. Note that Program Files (x86) will need to be in double quotes if it s part of the patch to WinSCP. Step 5: Make a quick batch file When I went through this process, Data Services seemed to have a little trouble with the quotes in the path to the WinSCP executable. To get around this, just put the executable command in a very simple batch file, and put that batch file somewhere in a path that has no spaces in it. In this example, you ll use C:\ftp_script\run_ftp.bat. It only needs to be one line: the command line you used from the previous step.
Test the batch file by calling it literally: C:\ftp_script\run_ftp.bat. If it runs, you re all good. Step 6: Run the file from Data Services Now, all you need to do in order to run the script from Data Services is to include it in a script object. Use the exec command in order to call the script. Everything else at this point should be automatic. I prefer to use batch mode 8 when running because it will return the contents of stdout, as well as information on the error if one occurs. Here s the simple script: print(exec('c:\ftp_script\run_ftp.bat', '',8)); This prints the results of the script to the console for easy viewing and debugging. Save and run your Data Services job. It should now be doing the file copy automatically. You can incorporate the script call into a larger Data Services job for whatever file transportation you need. Happy file moving! Britton Gray, Business Intelligence Consultant Decision First Technologies britton.gray@decisionfirst.com Britton Gray has been working in software development and Business Intelligence for the past fourteen years, and has been working with SAP BusinessObjects reporting, analytics, and information management tools for six years. He is certified in Data Integrator and Web Intelligence, and enjoys developing end-to-end BI solutions that enable customers to gain new insights into their data.