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

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

Introduction

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

Prototype

public boolean isConnected() 

Source Link

Document

Returns true if the client is currently connected to a 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.
 * /*www .j a va  2  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;
}