title: Implementing JSch to run ssh command in android app
body: <p>I'm currently trying to code up an android app that does a simple "ls" command over ssh to a remote machine. I've tried to use JSch and <a href="http://www.eridem.net/android-tip-021-ssh-execute-remote-commands-with-android" rel="nofollow">this page</a>, but it just doesn't seem to work. I've looked around and it seems that I have to implement some Async task to run the code. Here is the entire code for the main activity right now:</p>
  
  <pre><code>package com.sshtest.sshtest;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.os.AsyncTask;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.TextView;
  import java.util.Properties;
  import com.jcraft.jsch.JSch;
  import com.jcraft.jsch.Session;
  import com.jcraft.jsch.Channel;
  import com.jcraft.jsch.ChannelExec;
  import com.jcraft.jsch.*;
  
  import java.io.InputStream;
  import java.io.ByteArrayOutputStream;
  import java.lang.String;
  
  
  
  
  public class MainActivity extends Activity {
  
      String string;
  
      @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }
  
  
  private class sshconnection extends AsyncTask&lt;String, Void, String&gt; {
  
      protected String doInBackground(String... params) {
  
          try {
              JSch jsch = new JSch();
              Session session = jsch.getSession("name", "ipaddress", 22);
              session.setPassword("password");
  
              // Avoid asking for key confirmation
              Properties prop = new Properties();
              prop.put("StrictHostKeyChecking", "no");
              session.setConfig(prop);
  
              session.connect();
  
              ChannelExec channel = (ChannelExec)session.openChannel("exec");
              channel.setCommand("ls");
              channel.connect();
  
              InputStream input = channel.getInputStream();
  
  
              channel.disconnect();
  
  
  
  
  
  
          } catch (Exception e) {
              System.out.println(e.getMessage());
          }
          return null;
  
  
      }
  
      @Override
      protected void onPostExecute(String result) {
          TextView txt = (TextView) findViewById(R.id.txtipinfo);
          txt.setText("Executed");
  
      }
  
  
  }
  </code></pre>
  
  <ul>
  <li>adding uses-permission android:name="android.permission.INTERNET" to AndroidManifest.xml</li>
  </ul>
  
  <p>This is my first attempt at any serious coding, if anyone can help it would be greatly appreciated. Thanks!</p>