我正在尝试开发一个用于与某些程序交互的界面。我有一台 Windows 计算机,它将保存界面,还有一个 Raspberry Pi,它将启动程序。
我必须通过 SSH 使用它们,然后与它们交互:
- 从一些文本字段中,我获取程序的参数,然后启动它,连接到 SSH 会话。程序停留在等待状态“输入消息:”。
- 然后用户必须能够在计算机的另一个文本字段中输入消息,以便在程序上使用该消息。这是问题所在。
这是用于 2 个覆盆子之间的无线通信!
事实上,我通过任何 Expect 库(expectJ、expect4J、expectIt)启动程序都没有困难,但之后我总是无法输入消息。
架构是:
public SSH(String host, String user, String password) throws Exception {
connection = new JSch();
try {
session = connection.getSession(user, host, 22);
session.setPassword(password);
connection.setConfig("StrictHostKeyChecking", "no");
session.connect();
channel = session.openChannel("shell");
channel.connect();
expect = new ExpectBuilder()
.withOutput(channel.getOutputStream())
.withInputs(channel.getInputStream(), channel.getExtInputStream())
.withEchoInput(System.out)
.withEchoOutput(System.out)
.withExceptionOnFailure()
.build();
} catch (JSchException e) {
System.out.println(e.getMessage());
throw new Exception("Error on session");
}
}
public void exec(String prog) {
try {
expect.sendLine(prog);
} catch (IOException e) {
e.printStackTrace();
}
}
public String send(String cmd){
try {
expect.sendLine(cmd);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
return "" 应该是程序结束时的确认。但是 send() 总是失败并作为 linux 命令行输入数据......!所以,我正在寻找一个解决方案,比如期望实例的“子期望”......就像这样:
SSH --
|-./udp xxxxx
-|"hello"
-|"132"
|-close()
有人有想法吗?
谢谢 !