Exploiting the OS/2 REXX FTP Function Set

General
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

TP: The Power to Join Heterogeneous Systems

In the past, enterprises have used a variety of methods to move data between different operating systems. For example, the AS/400’s Send Network file (SNDNETF) command can transfer files between SNA-networked AS/400s or to SNA-connected mainframes. From a workstation, among other techniques, you can transfer files to MVS- and VM-based systems using IND$FILE. Unfortunately, most users have never had a single method that could be used to transfer files between all heterogeneous systems. There was a method available, but it wasn’t widely used. Today—with the increased acceptance of TCP/IP—this tool runs on practically every operating system. This “new” file transfer standard goes by the very short name of FTP.

Most users are familiar with FTP. In fact, you might already be using its archaic command-line interface to GET or PUT a file, perhaps even to list (ls) a file’s members or to print a working directory (pwd). Maybe you’ve appreciated FTP’s power, but found it lacking in the area of usability. OS/2’s Presentation Manager-enhanced FTP interface, FTPPM, certainly makes accessing files easier with its GUI. But why stop there? Why not tap into the power and flexibility of FTP via its REXX API? This article shows you how to do just that.

The OS/2 REXX FTP API

REXX is not exactly the language of choice among IT propeller heads, but it’s easy to use, flexible, and is supported by many add-on function sets. It’s also IBM’s Systems Application Architecture (SAA) procedure language, which means that the REXX skills you develop on the AS/400 can be used on OS/2- and OS/390-based systems, as well as on the many non-IBM platforms that support REXX, including Windows NT. The


OS/2REXX FTP function set lets a REXX program invoke many of the common FTP functions programmatically.

Usage Scenarios

Imagine the possibilities available with a programmable interface to FTP from your workstation. Your REXX application might, for example, transfer a unique set of files at a specified interval between the client workstation and a server (an AS/400, another workstation, a mainframe, or any system running the FTP daemon). This transfer could be used as part of a backup plan or a synchronization process, i.e., a REXX program can get a unique set of files every twelve hours as a backup, or it can compare the files’ contents and keep only the newer ones to synchronize files on the host and workstation on a more frequent basis.

A particularly powerful OS/2 REXX FTP function is FtpProxy, which lets a workstation user orchestrate file transfers between two host machines.

The sample REXX program in Figure 1 and its interface shown in Figure 2 illustrate how simple it is to connect to a server and display or download files. The program makes use of a simple character-based display, but don’t stop there! Go crazy; exploit VREXX and design your own customized file transfer interface.

REXX FTP Function Set

If you’re already familiar with FTP, most of the REXX FTP API functions are intuitive. For example, where you would manually type get, your REXX program will call FtpGet; where you would type ls to list file/member names on the server, your REXX program will call FtpLs. Even if you’ve never FTPed a file in your life, you’ll find the OS/2 REXX FTP function set quite understandable:

• FtpLoadFuncs and FtpDropFuncs—These functions load and unload the registered REXX FTP function set as part of normal REXX housekeeping. Refer to the sample REXX program for syntax.

• FtpVersion—FtpVersion(varname) sets varname to a string representing the version of the REXX FTP API package installed.

• FtpSetUser and FtpLogoff—FtpSet- User(server,user,pswd,account) specifies the server, user, password, and optional account information used by subsequent REXX FTP functions. Note that FtpSetUser doesn’t actually log on to the remote server; the logon occurs with the execution of the next FTP function requiring service, e.g., FtpPwd or FtpDir. FtpLogoff() ends the FTP session with the server named on a prior FtpSetUser call and resets the server, user, password, and account information specified with that call. Refer to the sample REXX program for syntax.

• FtpLs and FtpDir—FtpLs(pattern, stemname) and FtpDir(pattern,stemname) display directory information for the current remote directory, FtpLs in short format and FtpDir in long format. The resulting file/member list is placed in the REXX stemmed variable passed to the function. The populated stem variable’s elements can then be interpreted as follows: stemname.0 is the number of data elements returned; stemname.1 to stemname.n are those elements. The pattern passed to FtpLs and FtpDir can be a qualified file or a mask where an asterisk (*) and question mark (?) determine which file descriptions are returned. The asterisk substitutes for any character or group of characters; the question mark substitutes for only a single character. Refer to Figure 1 for sample usage.

• FtpPwd—FtpPwd(varname) or Print Working Directory sets varname to a string representing the current working directory.

• FtpChDir—FtpChDir(chdir) changes the working directory on the remote host to the directory specified in the variable chdir. Standard FTP directory navigation rules apply.


• FtpMkDir—FtpMkDir(newdir) creates a new directory on the remote host with the name specified in the variable newdir.

• FtpRmDir—FtpRmDir(olddir) removes the existing directory from the remote host specified in the variable olddir.

• FtpSetBinary—FtpSetBinary(“Binary “|”Ascii”) sets the default text translation for functions that transfer files, i.e., FtpGet, FtpPut, FtpAppend, and FtpPutUnique.

• FtpAppend—FtpAppend(Lfile,Rfile) appends a local file (Lfile) to the named remote file on the server (Rfile).

• FtpDelete—FtpDelete(Rfile) deletes the file Rfile on the remote host.
• FtpRename—FtpRename(oldname, newname) renames a file on the server.
• FtpGet—FtpGet(Lfile,Rfile) copies a file from the server (Rfile) to the local workstation (Lfile).

• FtpPut—FtpPut(Lfile,Rfile) copies a file from the local workstation (Lfile) to the server (Rfile).

• FtpPutUnique—FtpPutUnique (Lfile,Rfile) copies a file from the workstation (Lfile) to the server (Rfile), ensuring the server file name is unique so that it will not overwrite an existing file.

• FtpQuote—FtpQuote(string) sends the string to a server if the server supports FTP commands via QUOTE. You can use FtpQuote to execute remote commands on the AS/400.

For an example of how this works, see the article titled “Exploiting Remote Command Execution Across IBM Operating System Platforms” in the September/October 1997 issue of Client Access/400 Expert. The references at the end of this article can also provide you with additional information.

• FtpSite—FtpSite(info) sends site-dependent information to the server if it supports the site command.

• FtpSys—The FtpSys(varname) sets varname to a description of the operating system running on the server.

• FtpProxy—This is a powerful function that can copy files from one server to another. Sample invocation: FtpProxy(host1, userid1, password1, account1, host2, userid2, password2, account2, file1, file2).

• FtpPing—FtpPing(server,packet_ length) pings the named server with a packet of the specified length. Refer to the “REXX FTP Function Return Codes” sidebar on page 25 for the unique FtpPing return codes.

Conclusion

FTP is a popular tool that can be used to transfer files across heterogeneous operating systems. It is generally accessed via a clumsy command-line interface or limited- function GUI, but the OS/2 REXX API unleashes the power of FTP, empowering a workstation user to programmatically list and transfer files between a workstation and a host or between two remote hosts.

References

The following references are broken down by subject matter: FTP, REXX, and VREXX.

FTP References

AS/400 TCP/IP Configuration and Reference (SC41-3420-x) “Exploiting Remote Command Execution Across IBM Operating System


Platforms,” Client Access/400 Expert, September/October 1997 IBM TCP/IP for MVS, User’s Guide (SC31-7136-x) IBM TCP/IP for OS/2: User’s Guide (SC31-6075-x) REXX FTP API (an .INF file available in the TCPIPDOC directory)

REXX References

OS/2 Procedures Language/2 REXX (an .INF file in the OS/2 Information folder) The REXX Language: A Practical Approach to Programming,

Michael Cowlishaw, Prentice Hall, Englewood Cliffs, New Jersey: 1990 The REXX Language page at IBM Hursley: http://rexx.hursley.ibm.com/rexx/

rexxibm.htm Newsgroup: comp.lang.rexx VREXX References “A Visual OS/2 REXX Primer,” Client Access/400 Expert,

January/February 1997 “A Visual OS/2 REXX Primer: Advanced Functions,” Client Access/400 Expert,

March/April 1997

/* rexx */
/* */

/* Copyright 1998 Chris Miksanek. */
/* All Rights Reserved. */
/* */

/* This file is supplied "AS IS". The author */
/* does not provide any warranties, express */
/* or implied, including, but not limited to, */
/* the implied warranty fitness for any */
/* particular purpose. */

exitrc = 4 /* Default bad rc */
mboxsize = 79 /* display box width */
displines = 18 /* screen real estate */

/* Register and load REXX FTP Functions */
Rc = RxFuncAdd(" FtpLoadFuncs","rxFtp","FtpLoadFuncs")
Rc = FtpLoadFuncs( NoCR)

/* Retrieve command line args */
parse arg FTPserver FTPuser FTPpswd

cls

/* Prompt for unsupplied parms */
if FTPserver = '' then do

say 'Enter server name'

parse pull FTPserver

end
if FTPuser = '' then do

say 'Enter user for' FTPserver

parse pull FTPuser

end
if FTPpswd = '' then do

say 'Enter password for' FTPserver||'.'||FTPuser

parse pull FTPpswd
cls /* clear after pswd typed */

end

/* Display REXX FTP function set version */
say ' '
ftpVersion( FTPv)
call Message_Box 'REXX FTP Version:'||FTPv

/* PING server, exit if server not available */


call Message_Box,

'PINGing '||FTPserver||' ... wait ...'
rc = FtpPing(FTPserver,16)
if datatype(rc) <> NUM then do call Message_Box,

'PING to ('||FTPserver||') failed... rc: '||rc

signal quit

end
call Message_Box 'PINGed in '||rc||' ms'

/* Set server and user information */
rc = FtpSetUser(FTPserver, FTPuser, FTPpswd)

/* Get initial working directory */
rc = FtpPwd(dirName)
If rc <> 0 then do call Message_Box 'FTPdir Failed, rc:' FTPERRNO

signal quit

end

Rc = FtpSys(ServerInfo) /* Get Server info*/

/* Loop here until user enters "END" */
do forever

cls

call Message_Box ServerInfo

call Message_Box dirName /* Display WD */

call Message_Box, /* Prompt */

'Enter new directory, [CR], or END'

parse pull NewDir /* Get new dir */

if translate(newDir) = 'END' then do

exitrc = 0 /* OK end rc */

signal quit /* quit */

end

if NewDir <> '' then do /* process new wd */

Rc = FtpChDir(NewDir) /* change wd */

If Rc <> 0 then do /* wd change OK? */

call Message_Box, /* nope, ignore */

'FTPchDir failed, rc: '||FTPERRNO||,

'. Press [ENTER] to continue'

pull x

iterate /* loop back */

end

else do

DROP dirName /* APAR#: PN80464 */

rc = FtpPwd(dirName) /* Get new WD */

If rc <> 0 then do /* OK? */

Say 'FTPPwd Failed, rc:' FTPERRNO

signal quit /* No, exit */

end

cls

call Message_Box dirName

end

end

/* Extract WD name from WD message */

parse var dirName . '"' realDir '"' .

cls

call Message_Box,

'Listing files in '||realDir||' ... wait ...'

Rc = FtpLs('*','FTPfiles.')

If Rc <> 0 then Say 'List Failed, rc:' FTPERRNO

else do

cls

if FTPfiles.0 < displines then max = FTPfiles.0

else max = displines /* screen size */

call Message_Box realDir||' files 1 to '||max

do i = 1 to FTPfiles.0 /* list directory */

say right(i,3)||'.' FTPfiles.i

/* If screen filled, pause and clear */

if i // displines = 0 then do

call Message_Box,

'Paused... press [ENTER] to continue'

pull x

cls

if FTPfiles.0 < i+displines then max = FTPfiles.0

else max = i+displines

call Message_Box,


realDir||' files '||i+1|| ' to '||max

end

end i

/* Prompt for d/l option */

call Message_Box,

'Specify file # to download to workstation',

'or press [ENTER] to continue'

pull x

if x <> '' then do /* d/l? */

cls

if ((x < 1) | (x > FTPfiles.0)) then do

call Message_Box, /* invalid file # */

'Invalid file number.',

'Press [ENTER] to continue'

pull x

end

else do

len = length(FTPfiles.x) /* Trim? */

if len > 8 then len = 8 /* yes */

locname = substr(FTPfiles.x,1,len)

call Message_Box,

'Getting file '||FTPfiles.x||,

' as '||locname||'.FTP'

rc = FtpGet(locname||'.FTP',FTPfiles.x,"Ascii")

cls

call Message_Box,

'FTP Get rc: '||FTPERRNO||,

'. Press [ENTER] to continue'

pull x

end /* d/l file */

end

end

end /* do forever */

Quit:

Rc = FtpLogoff() /* House */

Rc = FtpDropFuncs() /* keeping */

call Message_Box 'Goodbye :-)'

exit exitrc

Message_Box:

parse arg mdata

mdata = left(mdata,mboxsize-4)

say '/'||left('',mboxsize-2,'ƒ')||'ø'

say '[.greaterequal] ' ||mdata||' [.greaterequal]'

say '¿'||left('',mboxsize-2,'ƒ')||'Ÿ'

return

/* */

Figure 1: Sample REXX program to view server directories and optionally download displayed files.


Exploiting_the_OS-2_REXX_FTP_Function_Set07-00.jpg 400x265

Figure 2: Results of sample REXX program displaying an OS/400 physical file. With so many files available for download, how do you choose?

REXX FTP Function Return Codes

The REXX FTP API return codes are slightly different than those of other functions. They’re a bit more difficult to handle, but a great deal more informative. FtpSetUser and FtpSetBinary are probably the least complicated, but then again the functions don’t do much, either. Each validates only the string passed. The functions return a value of 1 if the string passed is valid and a value of 0 if it is invalid. Remember that FtpSetUser doesn’t actually log on to the server, so a good return code at that point doesn’t really mean much. FtpPing is a unique function. It can return a numeric or a descriptive character string (refer to the sample REXX program for FtpPing return code interpretation). If the return code is numeric, it means the ping was successful and the numeric value is the number of milliseconds the ping took to return. If the value is nonnumeric, it will be one of the following:
• PINGREPLY—The server did not reply.
• PINGSOCKET—Ping was unable to obtain a socket.
• PINGPROTO—Unknown protocol.
• PINGSEND—The ping send failed.
• PINGRECV—The ping receive failed.
• PINGHOST—Attempting to ping an unknown host All of the other REXX FTP API functions return either a 0 for a successful call or a -1 for a failure. If the function returns a -1, the REXX variable FTPERRNO will contain one of the following:
• FTPSERVICE—The function was requesting an unknown service.
• FTPHOST—The function was attempting to connect to an unknown host.
• FTPSOCKET—The function was unable to obtain a socket.
• FTPCONNECT—The function was unable to connect to the server.
• FTPLOGIN—The function’s login failed.
• FTPABORT—The file transfer stopped.
• FTPLOCALFILE—There was a problem opening the local file.
• FTPDATACONN—There was a problem initializing the data connection.
• FTPCOMMAND—The command failed.
• FTPPROXYTHIRD—A proxy server does not support third-party transfers.
• FTPNOPRIMARY—There was no primary connection for the proxy transfer.


BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$