DOCUMENT ID:  1029-02

SYNOPSIS:     sample program segment showing use of getservbyname and rexec

OS RELEASE:   2.1, 2.4, 2.5 

PRODUCT:      Solaris

KEYWORDS:     sample program segment getservbyname rexec


DESCRIPTION:

The sample program segment demonstrates how to use the remote command
execution function.  The course of actions includes:


SOLUTION:

Establishing the DARPA port for "exec" facility and, executing the
command on the remote system using the rexec function. 

A point to remember when inspecting the rexec parameters is the coding
of "**x".  The familiar declaration of "main(argc,argv)", argv is coded
in one of two ways:


  *argv[]; /* an array of pointers to strings    */
  **argv;  /* a pointer to a pointer to a string */
 /**************************************************
 *       Sample program segment using rexec function
 ***************************************************/
 #include 
 #include 
 extern int errno;
 main()
 {
 char *ahost[1]; /* pointer to address of host to contact   */
 int  inport;    /* service port number                     */
 int  rem;       /* file descriptor to communucate to remote*/

 /*
 *  getservbyname
 *
 * The /etc/services file is searched looking first for "exec"
 * once found then look for "tcp"
 * when a match is found for both, the port number will be
 * returned
 *
 */
 inport = getservbyname("exec","tcp"); /* get service port */
 if (inport == NULL)            /* if NULL invalid service */
 {
       fprintf(stderr,"Get serve by name failed\n");
       exit(errno);
        }
 /*
 *  Set up a pointer to the remote host name
 *  note that ahost is declared as an array of pointers to
 *  strings.
 *
 *  remote is the sample name for a remote host and must be
 *  replaced by a real name.
 *
 */
        ahost[0] = "remote";
 /*
 *  Make the "rexec" system call
 *
 *  Upon successful completion rem will be connected to the
 *  stdin and stdout of the remote process. A bidirectional
 *  conduit now exists transact information.
 w
 *  In this case stderr will be attached to stdout.
 *
 *  rem will be -1 if an error is encountered executing the
 *  command on the remote host.
 *  Several error may be displayed on stderr of this current
 *  process
 *
 *  message                            reason
 *
 *  xxx: unknown host                  gethostbyname   failed
 *  getsockname: ...........           getsockname     failed
 *  accept:      ...........           accept          failed
 *  xxx:         ...........           reading stream  failed
 *
 *  Notes: xxx        is the remote host name
 *         .......... standard system error messages
 */
 rem = rexec(ahost,    /* address of array of pointers     */
 inport,               /* service port for network service */
 "account",            /* log in as account                */
 "my1pass2",           /* password                         */
 "recmd",              /* remote command                   */
 0);                   /* we don't want a control port     */
                       /* stderr will be the same as stdout*/
 /*
 * The remainder of the program is up to the users needs.
 */


DATE APPROVED: 09/12/95