Using the Rsh class |
You may create a new Rsh instance providing the remote host, username, local username command and error stream arguments to the constructor. The username represents the user whose credentials are to be used to execute any commands whereas the local username is the user who you will login as. In many cases these may be the same. The command is the command you wish to execute and the last argument determines whether the error stream should be initialized separately.
Example
Rsh rsh = new Rsh("remote.host.com", "johndoe", "johndoe","ls -la", false); try { // establish connection rsh.connect();
// execute command rsh.execute();
// get input stream InputStream input = rsh.getInputStream();
// read first byte to determine success or failure int in = input.read(); StringBuffer buffer = new StringBuffer(); if(in == 0) { buffer.append("Success! Remote host returned: \n"); } else { buffer.append("Failure! Remote host returned: \n"); }
// read response while((in = input.read()) != -1) { buffer.append((char)in); } System.out.println(buffer.toString()); } catch(Exception e) { System.out.println(e); } |