Example usage for org.apache.commons.net.bsd RCommandClient getErrorStream

List of usage examples for org.apache.commons.net.bsd RCommandClient getErrorStream

Introduction

In this page you can find the example usage for org.apache.commons.net.bsd RCommandClient getErrorStream.

Prototype

public InputStream getErrorStream() 

Source Link

Document

Returns the InputStream from which the standard error of the remote process can be read if a separate error stream is requested from the server.

Usage

From source file:org.squale.welcom.outils.rsh.Impl.JavaRshClient.java

/**
 * Retourne le resultat d'un commande Unix en rsh Attention : le buffer est limit a 1024.
 * /*from   w  w  w  .j a  v  a2 s.  co m*/
 * @param cmd : Commande unix
 * @param buff : Ecrit dans l'entree standard le contenu
 * @throws IOException : Retourne le buffer rcp ou bien une erreur d'execution
 * @return resultat unix
 */
public int executecmd(final String cmd, final byte buff[]) throws IOException {
    lastReturnStream = null;
    lastErrorStream = null;

    final RCommandClient rsh = new RCommandClient();

    // Stocke ce que l'on a envoyer
    addMessage(">" + cmd + "\n");

    try {
        rsh.connect(serveur);
        rsh.rexec(loginLocal, loginDistant, cmd + "\n", true);

        // Si on a quelquechose dans le buffer
        if (buff != null) {
            CopyUtils.copy(buff, rsh.getOutputStream());
            addMessage(buff);
            rsh.getOutputStream().close();

            // Faut etre dconnecter avant de lire
            if ((rsh != null) && rsh.isConnected()) {
                rsh.disconnect();
            }
        }

        if (rsh.getInputStream() != null) {
            lastReturnStream = IOUtils.toString(rsh.getInputStream());
            addMessage(lastReturnStream);
        }

        if (rsh.getErrorStream() != null) {
            lastErrorStream = IOUtils.toString(rsh.getErrorStream());

            if (lastErrorStream.length() > 0) {
                addMessage(lastErrorStream);

                return 1;
            }
        }
    } catch (final IOException ioe) {
        addMessage(ioe.getMessage());
        throw ioe;
    } finally {
        if ((rsh != null) && rsh.isConnected()) {
            rsh.disconnect();
        }
    }

    addMessage(">OK\n");

    return 0;
}