org.apache.commons.net.bsd

## Class RCommandClient

- java.lang.Object
- - org.apache.commons.net.SocketClient
  - - org.apache.commons.net.bsd.RExecClient
    - - org.apache.commons.net.bsd.RCommandClient

- Direct Known Subclasses:
    RLoginClient

      public class RCommandClient
      extends RExecClient

  RCommandClient is very similar to `RExecClient`, from which it is derived, and implements the rcmd() facility that first appeared in 4.2BSD Unix. rcmd() is the facility used by the rsh (rshell) and other commands to execute a command on another machine from a trusted host without issuing a password. The trust relationship between two machines is established by the contents of a machine's /etc/hosts.equiv file and a user's .rhosts file. These files specify from which hosts and accounts on those hosts rcmd() requests will be accepted. The only additional measure for establishing trust is that all client connections must originate from a port between 512 and 1023. Consequently, there is an upper limit to the number of rcmd connections that can be running simultaneously. The required ports are reserved ports on Unix systems, and can only be bound by a process running with root permissions (to accomplish this rsh, rlogin, and related commands usualy have the suid bit set). Therefore, on a Unix system, you will only be able to successfully use the RCommandClient class if the process runs as root. However, there is no such restriction on Windows95 and some other systems. The security risks are obvious. However, when carefully used, rcmd() can be very useful when used behind a firewall.

  As with virtually all of the client classes in org.apache.commons.net, this class derives from SocketClient. But it overrides most of its connection methods so that the local Socket will originate from an acceptable rshell port. The way to use RCommandClient is to first connect to the server, call the `rcommand()` method, and then fetch the connection's input, output, and optionally error streams. Interaction with the remote command is controlled entirely through the I/O streams. Once you have finished processing the streams, you should invoke `disconnect()` to clean up properly.

  By default the standard output and standard error streams of the remote process are transmitted over the same connection, readable from the input stream returned by `getInputStream()` . However, it is possible to tell the rshd daemon to return the standard error stream over a separate connection, readable from the input stream returned by `getErrorStream()` . You can specify that a separate connection should be created for standard error by setting the boolean `separateErrorStream` parameter of `rcommand()` to `true` . The standard input of the remote process can be written to through the output stream returned by `getOutputStream()` .

  See Also:
    `SocketClient`, `RExecClient`, `RLoginClient`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_PORT` The default rshell port.
    `static int` | `MAX_CLIENT_PORT` The largest port number an rcmd client may use.
    `static int` | `MIN_CLIENT_PORT` The smallest port number an rcmd client may use.

    - ### Fields inherited from class org.apache.commons.net.bsd.RExecClient

      `_errorStream_, NULL_CHAR`

    - ### Fields inherited from class org.apache.commons.net.SocketClient

      `_defaultPort_, _hostname_, _input_, _output_, _serverSocketFactory_, _socket_, _socketFactory_, _timeout_, connectTimeout, NETASCII_EOL`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `RCommandClient()` The default RCommandClient constructor.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `connect(InetAddress host, int port)` Opens a Socket connected to a remote host at the specified port and originating from the current host at a port in a range acceptable to the BSD rshell daemon.
    `void` | `connect(InetAddress host, int port, InetAddress localAddr)` Opens a Socket connected to a remote host at the specified port and originating from the specified local address using a port in a range acceptable to the BSD rshell daemon.
    `void` | `connect(InetAddress host, int port, InetAddress localAddr, int localPort)` Opens a Socket connected to a remote host at the specified port and originating from the specified local address and port.
    `void` | `connect(String hostname, int port)` Opens a Socket connected to a remote host at the specified port and originating from the current host at a port in a range acceptable to the BSD rshell daemon.
    `void` | `connect(String hostname, int port, InetAddress localAddr)` Opens a Socket connected to a remote host at the specified port and originating from the specified local address using a port in a range acceptable to the BSD rshell daemon.
    `void` | `connect(String hostname, int port, InetAddress localAddr, int localPort)` Opens a Socket connected to a remote host at the specified port and originating from the specified local address and port.
    `void` | `rcommand(String localUsername, String remoteUsername, String command)` Same as `rcommand(localUsername, remoteUsername, command, false);`
    `void` | `rcommand(String localUsername, String remoteUsername, String command, boolean separateErrorStream)` Remotely executes a command through the rshd daemon on the server to which the RCommandClient is connected.

    - ### Methods inherited from class org.apache.commons.net.bsd.RExecClient

      `disconnect, getErrorStream, getInputStream, getOutputStream, isRemoteVerificationEnabled, rexec, rexec, setRemoteVerificationEnabled`

    - ### Methods inherited from class org.apache.commons.net.SocketClient

      `_connectAction_, addProtocolCommandListener, applySocketAttributes, connect, connect, createCommandSupport, fireCommandSent, fireReplyReceived, getCharset, getCharsetName, getCommandSupport, getConnectTimeout, getDefaultPort, getDefaultTimeout, getKeepAlive, getLocalAddress, getLocalPort, getProxy, getReceiveBufferSize, getRemoteAddress, getRemotePort, getSendBufferSize, getServerSocketFactory, getSoLinger, getSoTimeout, getTcpNoDelay, isAvailable, isConnected, removeProtocolCommandListener, setCharset, setConnectTimeout, setDefaultPort, setDefaultTimeout, setKeepAlive, setProxy, setReceiveBufferSize, setSendBufferSize, setServerSocketFactory, setSocketFactory, setSoLinger, setSoTimeout, setTcpNoDelay, verifyRemote`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_PORT

          public static final int DEFAULT_PORT

      The default rshell port. Set to 514 in BSD Unix.
      See Also:
        Constant Field Values

    - #### MIN_CLIENT_PORT

          public static final int MIN_CLIENT_PORT

      The smallest port number an rcmd client may use. By BSD convention this number is 512.
      See Also:
        Constant Field Values

    - #### MAX_CLIENT_PORT

          public static final int MAX_CLIENT_PORT

      The largest port number an rcmd client may use. By BSD convention this number is 1023.
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### RCommandClient

          public RCommandClient()

      The default RCommandClient constructor. Initializes the default port to `DEFAULT_PORT` .

  - ### Method Detail

    - #### connect

          public void connect(InetAddress host,
                              int port,
                              InetAddress localAddr)
                       throws SocketException,
                              BindException,
                              IOException

      Opens a Socket connected to a remote host at the specified port and originating from the specified local address using a port in a range acceptable to the BSD rshell daemon. Before returning, `_connectAction_()` is called to perform connection initialization actions.
      Parameters:
        `host` - The remote host.
        `port` - The port to connect to on the remote host.
        `localAddr` - The local address to use.
      Throws:
        `SocketException` - If the socket timeout could not be set.
        `BindException` - If all acceptable rshell ports are in use.
        `IOException` - If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.

    - #### connect

          public void connect(InetAddress host,
                              int port)
                       throws SocketException,
                              IOException

      Opens a Socket connected to a remote host at the specified port and originating from the current host at a port in a range acceptable to the BSD rshell daemon. Before returning, `_connectAction_()` is called to perform connection initialization actions.
      Overrides:
        `connect` in class `SocketClient`
      Parameters:
        `host` - The remote host.
        `port` - The port to connect to on the remote host.
      Throws:
        `SocketException` - If the socket timeout could not be set.
        `BindException` - If all acceptable rshell ports are in use.
        `IOException` - If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.

    - #### connect

          public void connect(String hostname,
                              int port)
                       throws SocketException,
                              IOException,
                              UnknownHostException

      Opens a Socket connected to a remote host at the specified port and originating from the current host at a port in a range acceptable to the BSD rshell daemon. Before returning, `_connectAction_()` is called to perform connection initialization actions.
      Overrides:
        `connect` in class `SocketClient`
      Parameters:
        `hostname` - The name of the remote host.
        `port` - The port to connect to on the remote host.
      Throws:
        `SocketException` - If the socket timeout could not be set.
        `BindException` - If all acceptable rshell ports are in use.
        `IOException` - If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
        `UnknownHostException` - If the hostname cannot be resolved.

    - #### connect

          public void connect(String hostname,
                              int port,
                              InetAddress localAddr)
                       throws SocketException,
                              IOException

      Opens a Socket connected to a remote host at the specified port and originating from the specified local address using a port in a range acceptable to the BSD rshell daemon. Before returning, `_connectAction_()` is called to perform connection initialization actions.
      Parameters:
        `hostname` - The remote host.
        `port` - The port to connect to on the remote host.
        `localAddr` - The local address to use.
      Throws:
        `SocketException` - If the socket timeout could not be set.
        `BindException` - If all acceptable rshell ports are in use.
        `IOException` - If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.

    - #### connect

          public void connect(InetAddress host,
                              int port,
                              InetAddress localAddr,
                              int localPort)
                       throws SocketException,
                              IOException,
                              IllegalArgumentException

      Opens a Socket connected to a remote host at the specified port and originating from the specified local address and port. The local port must lie between `MIN_CLIENT_PORT` and `MAX_CLIENT_PORT` or an IllegalArgumentException will be thrown. Before returning, `_connectAction_()` is called to perform connection initialization actions.
      Overrides:
        `connect` in class `SocketClient`
      Parameters:
        `host` - The remote host.
        `port` - The port to connect to on the remote host.
        `localAddr` - The local address to use.
        `localPort` - The local port to use.
      Throws:
        `SocketException` - If the socket timeout could not be set.
        `IOException` - If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
        `IllegalArgumentException` - If an invalid local port number is specified.

    - #### connect

          public void connect(String hostname,
                              int port,
                              InetAddress localAddr,
                              int localPort)
                       throws SocketException,
                              IOException,
                              IllegalArgumentException,
                              UnknownHostException

      Opens a Socket connected to a remote host at the specified port and originating from the specified local address and port. The local port must lie between `MIN_CLIENT_PORT` and `MAX_CLIENT_PORT` or an IllegalArgumentException will be thrown. Before returning, `_connectAction_()` is called to perform connection initialization actions.
      Overrides:
        `connect` in class `SocketClient`
      Parameters:
        `hostname` - The name of the remote host.
        `port` - The port to connect to on the remote host.
        `localAddr` - The local address to use.
        `localPort` - The local port to use.
      Throws:
        `SocketException` - If the socket timeout could not be set.
        `IOException` - If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
        `UnknownHostException` - If the hostname cannot be resolved.
        `IllegalArgumentException` - If an invalid local port number is specified.

    - #### rcommand

          public void rcommand(String localUsername,
                               String remoteUsername,
                               String command,
                               boolean separateErrorStream)
                        throws IOException

      Remotely executes a command through the rshd daemon on the server to which the RCommandClient is connected. After calling this method, you may interact with the remote process through its standard input, output, and error streams. You will typically be able to detect the termination of the remote process after reaching end of file on its standard output (accessible through `getInputStream()` . Disconnecting from the server or closing the process streams before reaching end of file will not necessarily terminate the remote process.

      If a separate error stream is requested, the remote server will connect to a local socket opened by RCommandClient, providing an independent stream through which standard error will be transmitted. The local socket must originate from a secure port (512 - 1023), and rcommand() ensures that this will be so. RCommandClient will also do a simple security check when it accepts a connection for this error stream. If the connection does not originate from the remote server, an IOException will be thrown. This serves as a simple protection against possible hijacking of the error stream by an attacker monitoring the rexec() negotiation. You may disable this behavior with `setRemoteVerificationEnabled()` .

      Parameters:
        `localUsername` - The user account on the local machine that is requesting the command execution.
        `remoteUsername` - The account name on the server through which to execute the command.
        `command` - The command, including any arguments, to execute.
        `separateErrorStream` - True if you would like the standard error to be transmitted through a different stream than standard output. False if not.
      Throws:
        `IOException` - If the rcommand() attempt fails. The exception will contain a message indicating the nature of the failure.

    - #### rcommand

          public void rcommand(String localUsername,
                               String remoteUsername,
                               String command)
                        throws IOException

      Same as `rcommand(localUsername, remoteUsername, command, false);`
      Parameters:
        `localUsername` - the local user
        `remoteUsername` - the remote user
        `command` - the command
      Throws:
        `IOException` - on error

Copyright © 2001–2021 The Apache Software Foundation. All rights reserved.