我想在windows计算机上发送一个带有java应用程序的简单命令,以激活raspberry Pi上的python脚本,该脚本应该控制raspberry pi的GPIO端口。
我不太喜欢Jsch,我的SSH连接已经工作了,但没有发送命令。
以下是我的实际代码:
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();
}
}
}发布于 2014-03-14 13:25:42
您应该使用ChannelExec.setCommand(java.lang.String)启动它,并等待Channel.isClosed()返回false。顺便说一句,JSch有一个例子:http://www.jcraft.com/jsch/examples/Exec.java.html。
https://stackoverflow.com/questions/22406137
复制相似问题