Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to run and display the result of a shell command ssh with JSch?
    primarykey
    data
    text
    <p>I try to use the library <strong><a href="http://www.jcraft.com/jsch/" rel="nofollow">JSch - Java Secure Channel</a></strong> make an ssh connection in my Android app, it works.</p> <p>Now I would like to execute a command and retrieve the result.</p> <p>I tried several methods that works best is this. However, this method works only in part, because for some reason I can not explain, my program stops at the end of my while loop, yet I'm the result of the command that appears in my log.</p> <p><strong>Here is my code :</strong> </p> <pre><code>public static String executeRemoteCommand(String username, String password, String hostname, int port) throws Exception { JSch jsch = new JSch(); Session session = jsch.getSession(username, hostname, port); session.setPassword(password); // Avoid asking for key confirmation Properties prop = new Properties(); prop.put("StrictHostKeyChecking", "no"); session.setConfig(prop); session.connect(); Channel channel = session.openChannel("shell"); channel.connect(); DataInputStream dataIn = new DataInputStream(channel.getInputStream()); DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream()); // send ls command to the server dataOut.writeBytes("ls\r\n"); dataOut.flush(); // and print the response String line = dataIn.readLine(); String result = line + "\n"; while ((line = dataIn.readLine()) != null) { result += line + "\n"; Log.i("TAG", "Line: "+line); } dataIn.close(); dataOut.close(); channel.disconnect(); session.disconnect(); return result; } </code></pre> <p>Does anyone else have a better way to run a command sheel with JSch?</p> <p>Thank you in advance !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload