title: Android SSH Example Code
body: <p>I want to create an android activity for setting up an SSH Session with a remote device (through Wifi) and executing some linux commands on the remote device.  Anyone got a quick, short example for connecting, authenticating and sending remote commands using <a href="https://github.com/kruton/connectbot/tree/master/src/com/trilead/ssh2" rel="noreferrer">Trilead</a> libraries in Android ? Connectbot source (the only place to find the source for the unmaintained library) is quite vast and time consuming to go through if one is just trying to do a quick SSH Connection/ Command execution. I had found the sshJ library earlier, which had nicely documented examples and tips but unfortunately Android lacks some Java.Util classes required for sshJ.</p>
  
  <p>I am looking for something in Trilead like (this is from the sshJ example I found earlier) :</p>
  
  <pre><code>final SSHClient ssh = new SSHClient();
  ssh.loadKnownHosts();
  
  ssh.connect("localhost");
  try {
      ssh.authPublickey(System.getProperty("user.name"));
      final Session session = ssh.startSession();
      try {
          final Command cmd = session.exec("ping -c 1 google.com");
          System.out.print(cmd.getOutputAsString());
          System.out.println("\n** exit status: " + cmd.getExitStatus());
      } finally {
          session.close();
      }
  
  } finally {
      ssh.disconnect();
  }
  </code></pre>