尝试在远程机器上运行命令(例如:检查文件是否存在)但收到此错误。在这一步失败 -
channel.connect(60000);com.jcraft.jsch.JSchException:无法在 com.jcraft.jsch.Request.write(Request.java:65) 在 com.jcraft.jsch.RequestExec.request(RequestExec. java:56) 在 com.jcraft.jsch.ChannelExec.start(ChannelExec.java:43) 在 com.jcraft.jsch.Channel.connect(Channel.java:152) 在 SampleTest.jcraftTest(SampleTest.java:143) 在SampleTest.main(SampleTest.java:23)
我正在使用的代码
JSch jsch = new JSch();
session = jsch.getSession("username", "host", 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications", "password,keyboard-interactive");
session.setConfig(config);
session.setPassword("password");
session.connect(60000);
System.out.println("session connected");
channel = session.openChannel("exec");
System.out.println("channel opened");
((ChannelExec) channel).setCommand("uname -a && date && uptime && who");
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect(60000);
System.out.println("channel connected");
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
ee.printStackTrace();
}
}