#include <arpa/rexec.h> int rexec(char **host, int port, char *user, char *password, char *command, int *errorDescriptor);
The rexec() function is used to open a connection to a remote host and send a user ID, password, and command to the remote host. The remote host verifies that the user ID and password are valid. The command is issued after the user ID and password are validated.
getservbyname("exec", "tcp");
The port returned by getservbyname() is the port on which the remote host is listening for incoming rexec() connections.
non-NULL | A second connection is set up and that a descriptor for it is placed in the errorDescriptor parameter. This connection provides standard error results of the remote command. This information also includes remote authorization failure if rexec() is unsuccessful. |
NULL | The standard error results of the remote command are the same as the standard output return value. |
rexec() returns an integer. Possible values are:
No authorization is required.
When the rexec() API fails, errno can be set to one of following:
[ECONNABORTED] | Connection ended abnormally. |
[ECONNREFUSED] | The destination socket refused an attempted
connect operation.
This error occurs when the rexec server on the remote system is not active. |
[ECONNRESET] | A connection with a remote socket was reset by
that socket. |
[EFAULT] | Bad address.
System detected an address which was not valid while attempting to access the address parameters. |
[EHOSTUNREACH] | A route to the remote host is not available. |
[EINTR] | Interrupted function call. |
[EINVAL] | Parameter not valid. |
[EMFILE] | Too many descriptors for this process. |
[ENFILE] | Too many descriptors in system. |
[EPIPE] | Broken pipe. |
[ETIMEDOUT] | A remote host did not respond within the timeout
period.
This error code is returned when connection establishment times out. No connection is established. A possible cause may be that the partner application is bound, but the partner application has not yet issued a listen(). |
[EUNATCH] | The protocol required to support address family
AF_INET, is not available at this time. |
[EUNKNOWN] | Unknown system state. |
See Code disclaimer information for information pertaining to code examples.
The following example shows how rexec() is used:
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <qtqiconv.h> #include <arpa/rexec.h> #include <errno.h> #define BufLen 256 void main() { int sd = -1, rc; int responseLen = BufLen; int outbytesleft = BufLen; int bytesRead, saveBytesRead; struct servent serv_ent; struct servent_data serv_ent_data; char inbuf[BufLen]; char outbuf[BufLen]; char *inbufPtr = (char *)inbuf; char *outbufPtr = (char *)outbuf; iconv_t cd; QtqCode_T toCode = {0,0,0,0,0,0}; /* Convert to job CCSID */ QtqCode_T fromCode = {819,0,0,1,0,0}; /* ASCII CCSID */ char *host; char remoteHost[256] = "remoteHost"; char user[32] = "userName"; char password[32] = "myPassword"; char cmd[256] = "commandToRun"; int *errordesc = NULL; /* Must zero this out before call or results will be unpredictable. */ memset(&serv_ent_data.serve_control_blk, 0x00, sizeof(struct netdb_control_block)); /* retrieve the rexec server port number */ rc = getservbyname_r("exec", "tcp", &serv_ent, &serv_ent_data); if (rc < 0) printf("getservbyname_r() failed with errno = %d\n",errno); host = remoteHost; errno = 0; /* Issue the rexec API */ sd = rexec(&host, serv_ent.s_port, user, password, cmd, errordesc); if (sd == -1) /* check if rexec() failed */ { if (errno) printf("rexec() failed with errno = %d\n",errno); else printf("Either the host does not exist or remote authentication failed.\n"); } else /* rexec() was successful */ { bytesRead = recv(sd, inbuf, responseLen, 0); if (bytesRead > 0) { saveBytesRead = bytesRead; inbuf[bytesRead-1] = 0; /* Null terminate */ /* translate from ASCII to EBCDIC */ cd = QtqIconvOpen(&toCode, &fromCode); iconv(cd, (unsigned char **)&inbufPtr, (unsigned int *)&bytesRead, (unsigned char **)&outbufPtr, (unsigned int *)&outbytesleft); iconv_close(cd); outbufPtr -= saveBytesRead; /* Reset the buffer pointers */ printf("%s\n",outbufPtr); } else if (bytesRead == 0) printf("The remote host closed the connection.\n"); else printf("recv() failed with errno = %d\n",errno); } if (sd != -1) close(sd); /* close the connection. */ return; }
Top | UNIX-Type APIs | APIs by category |