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

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

Introduction

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

Prototype

@Override
public void disconnect() throws IOException 

Source Link

Document

Disconnects from the server, closing all associated open sockets and streams.

Usage

From source file:examples.rshell.java

public static final void main(String[] args) {
    String server, localuser, remoteuser, command;
    RCommandClient client;

    if (args.length != 4) {
        System.err.println("Usage: rshell <hostname> <localuser> <remoteuser> <command>");
        System.exit(1);/*from w  w w.j a va 2s.  c  om*/
        return; // so compiler can do proper flow control analysis
    }

    client = new RCommandClient();

    server = args[0];
    localuser = args[1];
    remoteuser = args[2];
    command = args[3];

    try {
        client.connect(server);
    } catch (IOException e) {
        System.err.println("Could not connect to server.");
        e.printStackTrace();
        System.exit(1);
    }

    try {
        client.rcommand(localuser, remoteuser, command);
    } catch (IOException e) {
        try {
            client.disconnect();
        } catch (IOException f) {
        }
        e.printStackTrace();
        System.err.println("Could not execute command.");
        System.exit(1);
    }

    IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), System.in, System.out);

    try {
        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }

    System.exit(0);
}

From source file:examples.unix.rshell.java

public static void main(String[] args) {
    String server, localuser, remoteuser, command;
    RCommandClient client;

    if (args.length != 4) {
        System.err.println("Usage: rshell <hostname> <localuser> <remoteuser> <command>");
        System.exit(1);//w ww  .j av a2 s. co m
        return; // so compiler can do proper flow control analysis
    }

    client = new RCommandClient();

    server = args[0];
    localuser = args[1];
    remoteuser = args[2];
    command = args[3];

    try {
        client.connect(server);
    } catch (IOException e) {
        System.err.println("Could not connect to server.");
        e.printStackTrace();
        System.exit(1);
    }

    try {
        client.rcommand(localuser, remoteuser, command);
    } catch (IOException e) {
        try {
            client.disconnect();
        } catch (IOException f) {
        }
        e.printStackTrace();
        System.err.println("Could not execute command.");
        System.exit(1);
    }

    IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), System.in, System.out);

    try {
        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }

    System.exit(0);
}

From source file:org.apache.commons.net.examples.unix.rshell.java

public static void main(String[] args) {
    String server, localuser, remoteuser, command;
    RCommandClient client;

    if (args.length != 4) {
        System.err.println("Usage: rshell <hostname> <localuser> <remoteuser> <command>");
        System.exit(1);/*w w  w.ja  v a 2  s.  c  o  m*/
        return; // so compiler can do proper flow control analysis
    }

    client = new RCommandClient();

    server = args[0];
    localuser = args[1];
    remoteuser = args[2];
    command = args[3];

    try {
        client.connect(server);
    } catch (IOException e) {
        System.err.println("Could not connect to server.");
        e.printStackTrace();
        System.exit(1);
    }

    try {
        client.rcommand(localuser, remoteuser, command);
    } catch (IOException e) {
        try {
            client.disconnect();
        } catch (IOException f) {
            /* ignored */}
        e.printStackTrace();
        System.err.println("Could not execute command.");
        System.exit(1);
    }

    IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), System.in, System.out);

    try {
        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }

    System.exit(0);
}

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 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;
}