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

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

Introduction

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

Prototype

public RCommandClient() 

Source Link

Document

The default RCommandClient constructor.

Usage

From source file:examples.rshell.java

public static final void main(String[] args) {
    String server, localuser, remoteuser, command;
    RCommandClient client;//from w w  w.  j a v  a 2s  . co  m

    if (args.length != 4) {
        System.err.println("Usage: rshell <hostname> <localuser> <remoteuser> <command>");
        System.exit(1);
        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;/*from   ww  w.  jav a2 s  . com*/

    if (args.length != 4) {
        System.err.println("Usage: rshell <hostname> <localuser> <remoteuser> <command>");
        System.exit(1);
        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:com.jkovacic.rclient.RshApache.java

/**
 * Establishes a connection to a r* daemon
 * //  w w w . j a v  a2 s  .  co  m
 * @throws RException if it fails
 */
public void connect() throws RException {
    // connection will only be established if it does not exist yet
    if (null != rshContext) {
        return;
    }

    // Check of credential validity
    if (null == cred) {
        throw new RException("No credentials provided");
    }

    // TODO further checking of credentials?

    try {
        rshContext = new RCommandClient();
        // TODO use isAvailable()?
        rshContext.connect(cred.getHostname(), cred.getPort());
    } catch (IOException ex) {
        cleanup();
        throw new RException("Connection to rsh service failed");
    }
}

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

public static void main(String[] args) {
    String server, localuser, remoteuser, command;
    RCommandClient client;//w  w w  . j a  v  a 2 s  .c  om

    if (args.length != 4) {
        System.err.println("Usage: rshell <hostname> <localuser> <remoteuser> <command>");
        System.exit(1);
        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  w ww. j  av  a 2  s.com
 * @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;
}