4

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".

I figured out how to use the following code using This. 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.

Here is the code (not everything included i hope this)

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<Integer, Void, Void>() {
                @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();
}
}

Thank you very much for your help, if i should post any further code or information please tell me.

Michael

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)?

Community
  • 1
  • 1
Michael Thumm
  • 81
  • 1
  • 8

1 Answers1

0

As per Multiple commands using JSch you could try:

channelssh.setCommand("cd Desktop;sudo java -classpath .:classes:/opt/pi4j/lib/'*' SimpleTextServer");
Community
  • 1
  • 1
Aidan
  • 1