Question

I want to send a simple command with a java application over my windows computer to activate a python script on my raspberry pi which should control the GPIO ports of my raspberry Pi.

I don't get really into Jsch, my SSH connection already works but not sending commands.

Here is my actual code:

package sshtest;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import com.jcraft.jsch.*; 
import java.io.InputStream;

public class SSHTest {


public static void main(String[] args) {
    String host="DELETED";
    String user="pi";
    String password="DELETED";
    String command =("cd Desktop");
    String command2 =("sudo python IO2.py all high");
    try{

        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");
        JSch jsch = new JSch();
        Session session=jsch.getSession(user, host, 22);
        session.setPassword(password);
        session.setConfig(config);
        session.connect();
        System.out.println("Connected");

        Channel channel = session.openChannel("exec");
        InputStream is = new ByteArrayInputStream(command.getBytes());
        channel.setInputStream(is);
        InputStream iss = new ByteArrayInputStream(command2.getBytes());
        channel.setInputStream(iss);





        session.disconnect();

    }catch(Exception e){
        e.printStackTrace();
    }
}
}
Was it helpful?

Solution

You should use ChannelExec.setCommand(java.lang.String) to start it, and wait while Channel.isClosed() returns false. BTW, JSch has an example for this: http://www.jcraft.com/jsch/examples/Exec.java.html.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top