title: execute SSH command on Rasperry Pi with Android App
body: <p>I am trying to send a command to my Raspberry Pi using an Android App with SSH. The command should only be performed by the Raspberry Pi, i don't need to save any OutputStreams. It is working when i send the command "sudo reboot", since the Pi is performing a reboot when i press the Button on the App. What i actually want to do is start a program by using first the command "cd Desktop" and then "sudo java -classpath .:classes:/opt/pi4j/lib/'*' SimpleTextServer".</p>
  
  <p>I figured out how to use the following code using <a href="https://stackoverflow.com/questions/14323661/simple-ssh-connect-with-jsch" title="This">This</a>. The external libs are included, the app itself is working, it's only about sending the commands to the Pi and that the Pi executes these commands. </p>
  
  <p>Here is the code (not everything included i hope this)</p>
  
  <pre><code>public class SlimpleTextClientActivity extends Activity implements OnClickListener{
  
      private Button buttonSSH;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_slimple_text_client);
  
      //Button press event listener
      buttonSSH.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              new AsyncTask&lt;Integer, Void, Void&gt;() {
                  @Override
                  protected Void doInBackground(Integer... params) {
                      try {
                          Ausgabe = executeRemoteCommand("pi", "raspberry", "38.110.23.254", 22);
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                      return null;
                  }
              }.execute(1);
          }
      });
  }
  
       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();
  
      // SSH Channel
      ChannelExec channelssh = (ChannelExec)
                  session.openChannel("exec");
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      channelssh.setOutputStream(baos);
  
      // Execute command
      //channelssh.setCommand("cd Desktop");
      //channelssh.setCommand("sudo java -classpath .:classes:/opt/pi4j/lib/'*' SimpleTextServer");
      channelssh.setCommand("sudo reboot");
      channelssh.connect();
      channelssh.disconnect();
  
      return baos.toString();
  }
  }
  </code></pre>
  
  <p>Thank you very much for your help, if i should post any further code or information please tell me. </p>
  
  <p>Michael</p>
  
  <p>edit: so i am sure the code itself is working, if i send the command "lsusb > home/pi/Text.txt" for example, it creates a text file at this directory with the output of the terminal. So how can i change the code that it only runs the command (and maybe shows it at the Terminal on the Raspberry Pi)?</p>